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

1 ビュー (過去 30 日間)
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
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.
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
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
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 件のコメント
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