Hopefully this is understandable...
I'm writing a function that requires two variables to be entered, M1 and M2. And defined it in a way such that M = [M1 M2] within the function. The equation required requires the use of M1 and M2.
So on my m-file page, is it possible to say that M = [3 4], such that the program can use M1 = 3, M2 = 4. I want to make it so that I don't have to enter the variables into my function file, but into the m-file instead, so that any value can be used for M1 and M2.
Is this possible?
Thank you.

 채택된 답변

Star Strider
Star Strider 2015년 2월 28일

0 개 추천

It is not only possible, it is the correct way to design your code, if I understand correctly what you want to do.
You would reference ‘M’ differently, though:
M1 = 3;
M2 = 4;
M = [M1, M2];
then to refer to the elements of ‘M’:
M1 = M(1);
M2 = M(2);
I’m not certain what you want to do, so if you’re asking something different than what I wrote, please clarify.

댓글 수: 2

Annalise
Annalise 2015년 2월 28일
Oh that's perfect! Thank you ^^
Star Strider
Star Strider 2015년 2월 28일
My pleasure!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 2월 28일

0 개 추천

You say "so that I don't have to enter the variables into my function file, but into the m-file instead" so I take it that you have two m-files , one is a "function file" and the other "m-file" is your script. So in your script I'd define the two variables
M1 = 3;
M2 = 4;
output = MyFunction(M1, M2);
Then in your "function file" "function that requires two variables to be entered, M1 and M2" , I'd have this where you list the two variables in the input argument list:
function output = MyFunction(M1, M2)
M = [M1, M2];
% more code that uses M....

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

질문:

2015년 2월 28일

댓글:

2015년 2월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by