Call variable to another function without GUI
2 views (last 30 days)
Show older comments
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
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?
Accepted Answer
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"
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
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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!