Function outputs inside another function

Hello everyone,
I am writing a script where I call a main function to update my variables. However this function has two outputs only and inside this function i call other functions with other outputs. So the outputs of the other functions are lost from workspace when the programms finishes as they are not output of my main function...
How can i also have all these outputs remaining in my workspace so that i can use them later in my script?

 采纳的回答

Why not extend the outputs of your main function to output those information you want?
If for some reason that you can't extend the output of your main function, there are still some options:
  1. Use global variables. Your "other" functions output to the global variables, so they can be stored.
  2. A more object-oriented approach, using singleton. Create a singleton outside your main function. Store all the information to the singleton, which will stay even after your main function returns.

6 个评论

Thanks a lot for your response Yongjian!
Can you please explain what do you mean by global variables or send me a link/reference of them ?
Can you please also refer me somewhere for singleton cause again it is something new to me.
Why not change your main function to return more output variables, e.g. in a struct? That would be mostly equivalent of using a custom class, which is what @Yongjian seems to be suggesing.
I strongly urge you not to use global variables whenever you can avoid them. They will cause trouble. It is like sharing a single backpack with your entire school: you will have to carefully check the contents every time you use it, and you can never be sure whether someone has messed with your data.
Try this for global variable
function [x, y] = gl() % the main fuction
% a global variable
global gVariable;
% main function returns these two variables
x = 0;
y = 1;
% put the output of other func into the global variable
gVariable = other_func();
end
function ret = other_func()
% other func returns this
ret = 100;
end
Now from the command line window:
>> [a, b] = gl
a = 0
b =1
>> global gVariable
>> gVariable
gVariable = 100
>>
Please note that global variable is not the best approach. If you want to use singleton, please refer to this answer:
https://www.mathworks.com/matlabcentral/answers/470303-single-instance-of-class
You have 63 characters to name your variable. Because a global is not unique to you, it might be used in other functions if you give it a general name.
If you are suggesting solutions using global, at least teach a naming convention that prevents collision, perhaps something like this:
global FunctionGroupOrToolboxName___myFunctionName___VariableName
VariableName=FunctionGroupOrToolboxName___myFunctionName___VariableName;
%
% rest of your function using VariableName
%
FunctionGroupOrToolboxName___myFunctionName___VariableName=VariableName;
Using a good naming convention is definitely a good idea.
Thank you both for your answers ! They are very helpful! I think i will use a structure eventually and that was what i was thinking initially. I was just wondering if there was a more neat way, but i think this is the neatest way possible and the most straightforward to implement.
The only problem with the structure arrays is the indexing and when you are using them as input in functions!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by