Call variable to another function without GUI

I would like to ask that how can I call or use a value of variables from one function to another function or script?
Suppose I have a function named example.m in which I have four variables P,S,X and Y. I would like to use the resultant values of P and S in another function or script named FinalResult.m Keeping in mind that both P and S have range of values, lets say S=0:0.01:5; X=2; Y=6; the resultant P has range of values for different values of S.
I am not using GUI. I also would not like to use Global variable as I am writing different functions so most of the time the variable names are similar and it could cause problem.
Could someone guide me on this issue please?
Thank you

2 Comments

Stephen23
Stephen23 on 19 Jul 2017
Edited: Stephen23 on 19 Jul 2017
Note that very basic MATLAB concepts, such as how to call functions and pass data between them is covered quite well in the introductory tutorials, which are highly recommended for all beginners:
You could also simply read the MATLAB documentation, which describes all of the ways that it is possible to pass data between function workspaces:
Note that the documentation clearly states Best Practice: Passing Arguments, and this is what you should do. Using globals, assignin, or evalin make code slow and buggy, and should be avoided:
"I am writing different functions so most of the time the variable names are similar and it could cause problem"
The names that variables have inside one workspace are irrelevant to their names when allocated to input or output arguments, so it is unclear what your concern is. For example: do you know what internal variables names sin uses? Does it matter to your code how many or what internal variables sin uses? Why should it make any difference to your functions?
Thank you for your reply. Before posting the question here I already went through the link which you have suggested,
https://www.mathworks.com/help/matlab/matlab_prog/share-data- between-workspaces.html
But I could not really get how shall I put the Passing Argument in my use. The example given at the link is not clear to me. Could you please explain it in my case?
With regard to the Global variable, as it says at the link that "Any function can access and update a global variable. Other functions that use the variable might return unexpected results." Thats why I mentioned that, and you have also suggested, to avoid using Global variable.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 19 Jul 2017
Edited: Stephen23 on 19 Jul 2017
" I have a function named example.m in which I have four variables P,S,X and Y. I would like to use the resultant values of P and S in another function"
So define those variables as output arguments, just like the function help shows:
function [P,S] = example(...)
...
P = ...
S = ...
end
"use the resultant values of P and S in another function or script named FinalResult.m"
and then you can simply get those values when you call example from within FinalResult:
[Pout,Sout] = example();
"Keeping in mind that both P and S have range of values, lets say S=0:0.01:5; X=2; Y=6; the resultant P has range of values for different values of S"
This seems to be unrelated to the task of passing values between workspaces, and it is unclear to me what relevance that has to your quetion.

8 Comments

@jack carter: please show the complete error message. This means all of the red text.
This is the complete error message
Not enough input arguments.
Error in example (line 12) LS=Le;
Error in FinalResult (line 44) [veffout] = example();
You defined your function example to require five inputs. Then you call it with zero inputs.
This is the function definition line for your function example:
function [P] = example(v,tau0,Gd,Le,beta)
And this is line 44 of FinalResult, where you call example:
[veffout] = example();
So you call example with zero input arguments, even though it requires five input arguments (clearly, because you use those input arguments inside the function itself).
I change the (line 44) to;
[veffout] = example(v,tau0,Gd,Le,beta);
Now I am getting the following error; Undefined function or variable 'veff'.
Error in FinalResult (line 46) plot(veff,P1)
It seems I have not called veff from function example correctly?
@jack carter: Look at your code:
[veffout] = example();
plot(veff,P1)
Where is veff defined in that workspace? Nowhere. Thus the error. You define a variable named veffout, but veffout remains totally unused.
Also note that within example you do not pass veff as an output argument, so of course it does not magically appear in another workspace outside of the one where it was defined. Look at how you defined example:
function [P] = example(v,tau0,Gd,Le,beta)
It has one output argument defined to be the internal variable called P. You do not specify veff as an output argument (but you could). Now look at how you called example:
[veffout] = example();
This will put the value that is P inside the function (because that is how you wrote the function) into a variable named veffout. Is that what you want?
Really input and output arguments are not that magical: they do not swap around, change variables, or perform any other weirdness. Once you define P as the output variable then P remains the output variable. It might be a good idea to do the MATLAB introductory tutorials, which are highly recommended for all beginners:
I think that would remove some of your confusion.
@Stephen Cobeldick: Isn't it what you suggested to pass P and S?
[Pout,Sout] = example();
I followed the same for veff before using the plot command in FinalResult
[veffout] = example(v,tau0,Gd,Le,beta);
@jack carter: I don't care what you call your variables, I just picked some random names. You could call them
[IlikeFlyingToTheMoon,MyAuntTeachesMaths] = example();
for all I care. You are turning this into a much more complicated thing that it really is. If the names had to follow some particular pattern based on the variable names that you used inside example do you think I would have mentioned this? Instead I told you that they are totally unrelated!
You obviously missed reading my very first comment. Here is the last part again: "The names that variables have inside one workspace are irrelevant to their names when allocated to input or output arguments, so it is unclear what your concern is. For example: do you know what internal variables names sin uses? Does it matter to your code how many or what internal variables sin uses? Why should it make any difference to your functions?"
What happens inside one workspace is totally irrelevant to what happens in another workspace. The only connection should be via input and output arguments. Once again for luck: the names you use for variables inside a function are irrelevant to the names you use for the arguments when calling that function.
Look at sin:
blah = sin(0.1);
will allocate the output argument into a variable named blah. Do you imagine that blah is the name of some variable used internally within sin? I have no idea what variables sin might have inside, nor does it matter. Oh look, now I got bored with blah and changed it to anna:
anna = sin(0.1);
The names given to an output argument do not matter and are unrelated to the names used internally within a function. That is the whole point of functions: they are a black box: put something in one end, something else comes out the other end, but what happens inside is totally irrelevant.
It might be a good idea to do the MATLAB introductory tutorials, which are highly recommended for all beginners:
I think that would remove some of your confusion.

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Asked:

on 19 Jul 2017

Edited:

on 19 Jul 2017

Community Treasure Hunt

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

Start Hunting!