Call variable to another function without GUI

조회 수: 2 (최근 30일)
jack carter
jack carter 2017년 7월 19일
편집: Stephen23 2017년 7월 19일
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
Stephen23
Stephen23 2017년 7월 19일
편집: Stephen23 2017년 7월 19일
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 2017년 7월 19일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2017년 7월 19일
편집: Stephen23 2017년 7월 19일
" 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
jack carter
jack carter 2017년 7월 19일
@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 2017년 7월 19일
편집: Stephen23 2017년 7월 19일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by