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 件のコメント

Jos (10584)
Jos (10584) 2013 年 10 月 16 日
What is the error?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 16 日
What is the error message?
dpb
dpb 2013 年 10 月 16 日
And show the calling statement and error in context...
ToTo
ToTo 2013 年 10 月 16 日
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)];
dpb
dpb 2013 年 10 月 16 日
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
ToTo 2013 年 10 月 16 日
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)];
Walter Roberson
Walter Roberson 2013 年 10 月 16 日
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
ToTo 2013 年 10 月 16 日
i call the script file "lorenz Solution.m"
ToTo
ToTo 2013 年 10 月 16 日
Walter I changed the name of the script file and it worked. But why is that
Yannick
Yannick 2013 年 10 月 16 日
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
Walter Roberson 2013 年 10 月 16 日
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
dpb 2013 年 10 月 16 日
And, all goes to show how not posting context extended the conversation far longer than necessary to finally uncover the problem... :(
ToTo
ToTo 2013 年 10 月 16 日
thank you very much every body. keep going
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 17 日
Keep going?
ToTo
ToTo 2013 年 10 月 18 日
i mean iwill keep going with my work in MatLab. Thank You all

サインインしてコメントする。

回答 (1 件)

Yannick
Yannick 2013 年 10 月 17 日

1 投票

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 件のコメント

dpb
dpb 2013 年 10 月 18 日
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
Yannick 2013 年 10 月 19 日
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 2013 年 10 月 19 日
編集済み: dpb 2013 年 10 月 19 日
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 2013 年 10 月 19 日
@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 2013 年 10 月 19 日
Not sure what that has to do with the proper description of allowable m-file naming though???

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeWorkspace Variables and MAT Files についてさらに検索

質問済み:

2013 年 10 月 16 日

コメント済み:

dpb
2013 年 10 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by