How to create variables in the command prompt space using a function

1 回表示 (過去 30 日間)
Buddhi Wimarshana
Buddhi Wimarshana 2012 年 9 月 6 日
I have a problem with passing a value of a variable (say X)in one function (function A) to another function (function B), because i can't pass the particular function arguments of the first function(A) inside the function B. So what I am trying to do is this. I can get the values of that variable(X)to the command prompt. Then I am trying to pass those values to the second function(B) as an argument. It's hard to put the real problem in here. I will give an example the following is my function A.
function [X] = funcA (inputsToA)
X; //this is the required variable to be passed to function B
end
my second function is as follows
function [outB] = funcB (inputsToB)
.
.
X = funcA (inputsToA) //i am trying to get X here, BUT 'inputsToA' has to be //generated in the function and it's not possible due to some reasons.
end
So what I am trying to do is this. First get the value of X to the command prompt simply as follows (by removing the semicolon at X)
function [X] = funcA (inputsToA)
X //remove the semicolon
end
and then try to get that value passed to the function B as follows.
function [outB] = funcB (inputsToB,X) //trying to get the value X end
So please be kind enough to show me a way to get this done...and apologize for the incompleteness of the problem. And I am new to MatLab programming. Thanks .

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 6 日
編集済み: Matt Fig 2012 年 9 月 6 日
First of all, comments in MATLAB are given by %, not //.
Second, which function are you calling first, and from where, and what are you passing in to that function? To pass a value to a function, you just pass it!
function [X] = funcA (inputsToA)
X = inputsToA;
funcB(X) %Pass X to function B
end
You are going to have to put a simplified version of the real problem so that we can understand what you want, I think. Start with two functions, funcA and funcB. Now what? You call funcA from the command line and need funcA to call funcB? Then what?
  2 件のコメント
Buddhi Wimarshana
Buddhi Wimarshana 2012 年 9 月 7 日
Hi Matt and yeah I totally messed up with C# commenting, thanks for your remind. Well if the two functions are in separate places then how can i pass the variable (value) to the second function? Yes of course I am calling the funcA from the comnd prompt and I need to pass variable X to funcA, and X is generated(I don't know the correct term to use) in funcB. But these two functions are in two different places, so as i know I need to pass the X to funcB as follows.
function [outA] = funcA(inputsToA)
[X] = funcB(inputsToB) %in this case i don't know the inputsToB.
end
I need to pass X to funcA from funcB. But I don't know the inputs to funcB in this case due to some reasons(it's hard to explain, so let's simply say I don't know them).
Matt, I am sorry about the confuse I made in the problem. Please be kind enough to help me in this matter. Thanks.
Matt Fig
Matt Fig 2012 年 9 月 7 日
You want to call funcA from the command line. In order to do that you need to have X, which is the return value from funcB. But then you say you need to pass X to funcB too?
I am not sure what you mean when you say that the functions are in two different places. If they are in two different m-files that are both in the same directory, then that is fine. But what is not fine is that funcB returns X, which is needed by funcA, but you cannot call funcB because you don't know what to pass to funcB to make it return X.
There is nothing I can do to help the current situation. Once you figure out what to pass to funcB in order to make it return X, simply do this:
X = funcB(mystery_inputs); % Get X from funcB
val = funcA(X); % pass X to funcA.

サインインしてコメントする。

その他の回答 (0 件)

製品

Community Treasure Hunt

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

Start Hunting!

Translated by