Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octave: invalid use of script in index expression

Tags:

plot

octave

I want to use a simple code (plot.m) in Octave to plot a figure. My code is the following:

printf('Plotting Data...\n');
x = -10:0.1:10;
plot(x, sin(x));

But I get the error message:

error: invalid use of script ex1/plot.m in index expression error: called from plot at line 3 column 1

Could you tell me how to solve it? Thanks!

like image 980
tktktk0711 Avatar asked Dec 20 '16 03:12

tktktk0711


2 Answers

I got the same problem, to solve it

  1. changed script name
  2. deleted the previous scripts (which are in old name and contain same scripts)
  3. write octave prompt the file name "without m" (write "file" instead of "file.m")

here is my script and named it "xx.m"

syms x;
f = x^3 - 6*x^2 + 11*x - 6; 
ezplot(f)
xlabel("x") 
ylabel("y") 
title ("name")
grid on

I write the octave prompt xx or run xx both work well.

In my opinion, the "invalid use of script" problem reasons are

  1. You should not name your script after your function. For exm., when name my script "ezplot.m" and it includes "ezplot(f)" it does not work. Change it names something other than your functions in your script
  2. If there are a number of m-file which includes the same script in a different name it causes the same error. Delete the other file which shares the same script.
like image 107
Z.Grey Avatar answered Nov 12 '22 15:11

Z.Grey


Short answer: Change the name of your script file.


Less short answer

When attempting to call a function somefunction(), Octave will first of all look for a file somefunction.m in your current directory.1 If it finds one, then it will attempt to call somefunction using this file. If it does not find one, then it will look for it among its built-in functions (stored somewhere else on your computer).

In your case, you attempt to call the plot() function. However, your script itself is called plot.m. So Octave first of all looks for plot.m in your current directory... and finds your script! It identifies that your plot.m file is a script and not a function. Scripts cannot be called with arguments (such as x and sin(x) in your case), which is why you get your "invalid use of script in index expression error" message.

The solution is therefore to change the name of your file to something other than plot.m.


1This assumes there is no variable in the current scope called somefunction. If there is, the variable takes precedence.

like image 34
jadhachem Avatar answered Nov 12 '22 13:11

jadhachem