How to show the output of a function when you call the code in another file?

6 次查看(过去 30 天)
Hello sir/mam, I am hereby to ask this question.
My question is that how to show the output of a self-made function from another file, without needing to write the "[x,y,z] = functionfromanotherfile(input)". To make it even clealer, I would like to say a file named "maincode.m" and a file named "functionA.m".
In the file "functionA.m", the first line of code is [x,y,z] = functionA(input);
and in the file "maincode.m", I wish to call the functionA(input); and directly generate the resulting outputs as x,y,z in the workspace. Butin the fille "maincode.m", if I didn't write it such as "[x,y,z] = functionA(input);" I will only get an answer in workspace of a single "answer" variable. So, how to make so that even though if I only write "functionA(input);" in maincode.m file, I can still generate those x,y,z variables in the workspace?
I hope the question is clear enough, and please let me know if it is not understandable. Thank you before!
  1 个评论
Stephen23
Stephen23 2022-1-13
编辑:Stephen23 2022-1-13
" To make it even clealer... I wish to call the functionA(input); and directly generate the resulting outputs as x,y,z in the workspace."
That is definitely not cleaner or clearer.
What you are describing requires magically making variables appear in another workspace, which is one way that beginners force themselves into writing slow, complex, inefficient, obfuscated code which is buggy and yet difficult to debug. Making variables magically appear in another workspace completely destroys any chance for the MATLAB JIT engine to speed up your code, it destroys any ability for you and the static code check to know where variables come from, and many other disadvantages:
What you are describing actually gets in the way of writing clean, neat, efficient, fast MATLAB code: passing variables as input/output arguments is the fastest, neatest, clearest, and by far the most efficient and most secure way to pass data between workspaces, as the MATLAB documentation clearly states:
"So, how to make so that even though if I only write "functionA(input);" in maincode.m file, I can still generate those x,y,z variables in the workspace?"
Ugh, do NOT do that. You will really stop both you and MATLAB from working efficiently.

请先登录,再进行评论。

回答(1 个)

Kshitij Chhabra
Kshitij Chhabra 2022-1-13
Hi,
You can try returning the answer as a structure and further using it down your code. The documentation of struct can be found here.
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by