How to show the output of a function when you call the code in another file?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Stephen23
el 13 de En. de 2022
Editada: Stephen23
el 13 de En. de 2022
" 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.
Respuestas (1)
Kshitij Chhabra
el 13 de En. de 2022
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!
0 comentarios
Ver también
Categorías
Más información sobre Debugging and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!