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.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Abr. de 2014

0 votos

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

9 comentarios

Jing Lin Ng
Jing Lin Ng el 21 de Abr. de 2014
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 el 21 de Abr. de 2014
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 el 22 de Abr. de 2014
Editada: Walter Roberson el 22 de Abr. de 2014
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 el 22 de Abr. de 2014
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 el 22 de Abr. de 2014
because there is an error when i run the second function file.
it says Input argument "A" is undefined.
Walter Roberson
Walter Roberson el 22 de Abr. de 2014
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 el 22 de Abr. de 2014
so what should I do to be able to use the variable A and B in my second function file?
Walter Roberson
Walter Roberson el 22 de Abr. de 2014
Those techniques can be used even when you are not using callback functions.
Jing Lin Ng
Jing Lin Ng el 23 de Abr. de 2014
alright thanks for you answer!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 21 de Abr. de 2014

Comentada:

el 23 de Abr. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by