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

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 Kommentare

What is the error?
What is the error message?
And show the calling statement and error in context...
this is the 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)];
Again, not enough context :(
Need the call statement and the full error in_context. There's nothing specifically wrong w/ the statement in isolation but there's no context to see where the problem may be nor even a full error of what ML thinks is wrong.
Answers only as good as input given -- we can't see your screen from here.
Sorry here it is:
??? Input argument "y" is undefined.
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)];
To check: if you copy the line
[t y] = rk4sys(@lorenz,[0 20],[5 5 5],0.03125);
into the command window then it works, but if you put the five lines into a .m file and give the name of the .m file then it does not? What name did you give to the .m file script, and how do you invoke the script ?
i call the script file "lorenz Solution.m"
Walter I changed the name of the script file and it worked. But why is that
ToTo, it looks like you have a space in your MATLAB file name. Does it work if you remove the space between "lorenz" and "Solution" and execute the script using ">> lorenzSolution" ?
script names (and function names) cannot have spaces in them. They must start with a letter, and after that there can be letters, digits, and underscore.
And, all goes to show how not posting context extended the conversation far longer than necessary to finally uncover the problem... :(
thank you very much every body. keep going
i mean iwill keep going with my work in MatLab. Thank You all

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Yannick
Yannick am 17 Okt. 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 Kommentare

The statement on file names isn't correct--Matlab supports any file name which is legal on the underlying file system. While it's an abomination to do use them imo, spaces are allowed (must enclose in paren's) as well as can start with numbers or underscores.
Function and script names must follow the same rules as to be valid variable names; that is true.
But, the limitations on file names themselves isn't so strict --
>> fid=fopen('_ a sample file that''s really bad.txt','w');
>> fprintf(fid,'%s','Some stuff');
>> fid=fclose(fid)
>> type '_ a sample file that''s really bad.txt'
Some stuff
>>
As can be seen, that actually works altho I'd strongly discourage doing such things...
Thanks for the additional information. Of course, you are right. I was trying not to over-complicate things :)
The main point I was trying to make is that, if you want your .m files ("MATLAB files") to be somewhat usable in MATLAB, don't put any spaces, dashes and so on in it. Stick to letters, numbers and underscores (and only letters for the first character).
And as you say, using similar "rules" for all your files (MATLAB-related or not) is probably good practice anyway!
dpb
dpb am 19 Okt. 2013
Bearbeitet: dpb am 19 Okt. 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...
@dpb While I am not aware of any "magic" filenames putting files in directories that start with @ or + can cause very unexpected behavior.
Not sure what that has to do with the proper description of allowable m-file naming though???

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 16 Okt. 2013

Kommentiert:

dpb
am 19 Okt. 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by