Call variable to another function without GUI

2 views (last 30 days)
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?
jack carter
jack carter on 19 Jul 2017
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
jack carter on 19 Jul 2017
@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);
Stephen23
Stephen23 on 19 Jul 2017
Edited: Stephen23 on 19 Jul 2017
@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)

Community Treasure Hunt

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

Start Hunting!