The command is working perfectly in the command window but not as a script
Ältere Kommentare anzeigen
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
Jos (10584)
am 16 Okt. 2013
What is the error?
Azzi Abdelmalek
am 16 Okt. 2013
What is the error message?
dpb
am 16 Okt. 2013
And show the calling statement and error in context...
ToTo
am 16 Okt. 2013
dpb
am 16 Okt. 2013
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.
ToTo
am 16 Okt. 2013
Walter Roberson
am 16 Okt. 2013
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 ?
ToTo
am 16 Okt. 2013
ToTo
am 16 Okt. 2013
Yannick
am 16 Okt. 2013
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" ?
Walter Roberson
am 16 Okt. 2013
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.
dpb
am 16 Okt. 2013
And, all goes to show how not posting context extended the conversation far longer than necessary to finally uncover the problem... :(
ToTo
am 16 Okt. 2013
Azzi Abdelmalek
am 17 Okt. 2013
Keep going?
ToTo
am 18 Okt. 2013
Antworten (1)
Yannick
am 17 Okt. 2013
1 Stimme
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
dpb
am 18 Okt. 2013
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...
Yannick
am 19 Okt. 2013
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!
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
am 19 Okt. 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
am 19 Okt. 2013
Not sure what that has to do with the proper description of allowable m-file naming though???
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!