how to create a input file?

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.

 Accepted Answer

Walter Roberson
Walter Roberson on 21 Apr 2014
You can use save() to create a .mat file that you later load() from.

9 Comments

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?
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
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?
You do not do that. The way you have defined A and B, they are private to the P1 function.
because there is an error when i run the second function file.
it says Input argument "A" is undefined.
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"
so what should I do to be able to use the variable A and B in my second function file?
Those techniques can be used even when you are not using callback functions.
alright thanks for you answer!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!