I have two function file (A & B). I would like to put all the variables that I found in function A file into an input file. That means all the variables from A function file is saved in that input file. So that B function can recall the variables from the input file. Does anyone know how to create the input file?? Million thanks.

 採用された回答

Walter Roberson
Walter Roberson 2014 年 4 月 21 日

0 投票

You can use save() to create a .mat file that you later load() from.

9 件のコメント

Jing Lin Ng
Jing Lin Ng 2014 年 4 月 21 日
Hi Walter Roberson thanks for your reply. may I know need to save the name of function A of save every variables that existed in function A file?
Walter Roberson
Walter Roberson 2014 年 4 月 21 日
Sorry, I am not sure what you are asking there.
If you want to save all variables in the workspace of a function, then you can do that with
save('TheMatFileNameHere.mat');
When you want to restore the variables it is best if you use something like
AVariables = load('TheMatFileNameHere.mat');
then
fieldnames(AVariables)
will tell you the names of all of the variables that were restored from the file, and you can recall them using structure notation. For example,
AVariables.x_median
would return the value that x_median had in the function at the time of the save().
You can also use, for example,
fn = fieldnames(AVariables);
for K = 1 : length(fn)
thisfield = fn{K};
fprintf('Variable %s had datatype %s\n', thisfield, class(Avariables.(thisfield)) );
end
Jing Lin Ng
Jing Lin Ng 2014 年 4 月 22 日
編集済み: Walter Roberson 2014 年 4 月 22 日
sorry for do not asking my question clearly.
my first function file is as below:-
function wind=P1(object)
N=length(object);
wind = zeros(1,N);
wind(1) = object(1);
A=2*rand+object
B=3*rand+object*
then my second function file is as below:-
function speed=P2(weight)
GeneratedYears=30;
Y=GeneratedYears;
A=A';
B=B';
speed=A+weight
That means I want to use the output of variables A and B from first function file on my second function file.
How I can call the variables from the first function file?
Walter Roberson
Walter Roberson 2014 年 4 月 22 日
You do not do that. The way you have defined A and B, they are private to the P1 function.
Jing Lin Ng
Jing Lin Ng 2014 年 4 月 22 日
because there is an error when i run the second function file.
it says Input argument "A" is undefined.
Walter Roberson
Walter Roberson 2014 年 4 月 22 日
The code you show would not produce that error. It could produce an error about "object" or "weight" as undefined input arguments, but your P2 routine would produce
Undefined function or variable 'A'
not a message about input argument"A"
Jing Lin Ng
Jing Lin Ng 2014 年 4 月 22 日
so what should I do to be able to use the variable A and B in my second function file?
Walter Roberson
Walter Roberson 2014 年 4 月 22 日
Those techniques can be used even when you are not using callback functions.
Jing Lin Ng
Jing Lin Ng 2014 年 4 月 23 日
alright thanks for you answer!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by