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!
I got the same problem, to solve it
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With