MATLAB Answers

The command is working perfectly in the command window but not as a script

13 views (last 30 days)
ToTo
ToTo on 16 Oct 2013
Commented: dpb on 19 Oct 2013
Hi all,
I try to run this script:
[t y] = rk4sys(@lorenz,[0 20],[5 5 5],0.03125);
plot(t,y)
plot(y(:,1), y(:,2)), xlabel('x'), ylabel('y')
plot(y(:,1), y(:,3)), xlabel('x'), ylabel('z')
plot(y(:,2), y(:,3)), xlabel('y'), ylabel('z')
Where the called function lorenz is:
function [ yp ] = lorenz( t,y )
yp = [-10*y(1)+10*y(2);28*y(1)-y(2)-y(2)*y(3);-(8/3)*y(3)+y(1)*y(2)];
end
it gives error:
Error in ==> lorenz at 3
yp = [-10*y(1)+10*y(2);28*y(1)-y(2)-y(2)*y(3);-(8/3)*y(3)+y(1)*y(2)];
However, when i copy that script in the command window it works.

  15 Comments

Sign in to comment.

Answers (1)

Yannick
Yannick on 17 Oct 2013
Just to close the loop on this, the bottom line is that MATLAB file names, just like MATLAB variable or function names, can only contain letters (both lower- and upper-case), digits and underscores. Additionally, they must start with a letter (i.e., cannot start with a digit or an underscore).
In particular, spaces and dashes (-) are not valid characters in function, script and variable names.

  5 Comments

Show 2 older comments
dpb
dpb on 19 Oct 2013
But saying something wrong doesn't simplify, it confounds is the point I was trying to make.
Just say that m-files must follow the rules for variable names since functions/scripts must and point out that Matlab resolves m-file functions by the corresponding file name, not the function name inside the m-file. Thus the filename for m-file must follow the variable-naming conventions.
ADDENDUM:
The "wrong" is to extend the m-file naming rules to all files; that could give a neophyte stumbling across the thread the wrong idea that if had set of computer-generated datafile names like 001.dat, 002.dat, etc., think couldn't process them in Matlab. A stretch, yeah, but may as well try to have what is in Answers precise if it's going to be here.
@Yannick -- if want to edit your above, I'd delete followups to clean this all up and eliminate the sidebar...
Daniel Shub
Daniel Shub on 19 Oct 2013
@dpb While I am not aware of any "magic" filenames putting files in directories that start with @ or + can cause very unexpected behavior.
dpb
dpb on 19 Oct 2013
Not sure what that has to do with the proper description of allowable m-file naming though???

Sign in to comment.


Translated by