simple question: open another m file

sorry to ask. if I sub 2 variables(x,y) from the currently running first.m into second.m, and second.m will return 2 variables (a,b) back to first.m. I should write this in first.m:
[a,b]=second(x,y);
PS.the location of first.m and second.m is the same.
error message is: ??? Attempt to execute SCRIPT second as a function: C:\second.m

 채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 5일

0 개 추천

You can only use that syntax if the first line of the file second.m (that is not whitespace or all comments) starts with the keyword "function", such as
function [p,q] = second(m,n)
If second.m does not start with "function" then it is called a "script". Scripts cannot be passed parameters and cannot return values. Scripts do not have dummy parameter names like functions do. On the other hand, scripts have access to all of the variables of the function that starts them, and can read and write to those variables directly.

댓글 수: 5

Hello Blower
Hello Blower 2011년 3월 5일
if i don't need return values, can i write like:
second(x,y);
thank you
Walter Roberson
Walter Roberson 2011년 3월 5일
You can do that if and only if "second" is defined as a function. If "second" is a script instead of a function, you cannot have anything after it (except comments) until the end of the statement.
Hello Blower
Hello Blower 2011년 3월 5일
how to define it is function or script?
Hello Blower
Hello Blower 2011년 3월 5일
I have sent you mentioned program. can you help me to check what cause the problem?
Thanks a lot.
Hello Blower
Hello Blower 2011년 3월 5일
Sorted. I found I have to add "function [a,b] = second(x,y)" at the first line of second.m to make it as a function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by