How to use variable of function in the main program
이전 댓글 표시
Hi all,
I have a main program Main.m. In the main I call a function [a,b,c]=func(data), I would like now to use the output a in the Main.m. how can I do it ?
Thanks for the help.
답변 (1개)
... I call a function [a,b,c]=func(data),...
So, then you have a in the calling workspace (as well as b and c); so you just reference them as any other variable. Presuming, of course, that func doesn't error when called with data.
% script continues from above...
[a,b,c]=func(data);
fprintf('Asuitableformatstring',a) % display the result variable a or any other use
...
댓글 수: 4
Adam
2014년 8월 2일
Image Analyst
2014년 8월 2일
편집: Image Analyst
2014년 8월 2일
OK, this made me laugh - it did give me cheers. Not sure if you're making a joke or is you're serious but of course you know, or should know, that you were supposed to replace Asuitableformatstring with %d, %s, or %f depending on if "a" is an integer, a string, or a floating point number (single or double). For example if you return a string, double, and integer you'd do this:
[strV1, dblV2, intV3] = func(data);
printf('%s\n%.8f\n%d\n', strV1, dblV2, intV3);
The \n mean that it puts in a line feed/carriage return so that the next number will be on a new line. Note that the names in the calling routine don't need to match the names you gave them in the function definition, as of course you already know if you've ever done any programming before. If you really didn't yet know any of what I told you, then you need to read this: http://www.mathworks.com/matlabcentral/answers/8026-best-way-s-to-master-matlab
Star Strider
2014년 8월 2일
... speaking of which, using sprintf or fprintf might also eliminate some frustration.
dpb
2014년 8월 2일
good catch...and also a chuckle... :)
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!