passing a mat file variable to a function

10 visualizaciones (últimos 30 días)
Bernhard
Bernhard el 24 de Feb. de 2014
Comentada: Stephen23 el 20 de Jul. de 2018
I have written a function myfun.m which works on a very large input variable var stored in a file myfile.mat in the current folder. If I first load var into the workspace and then call myfun(var), var requires memory of twice its size because it is kept both in the base and the function workspace. How can I instead pass just the names of myfile.mat and var on calling myfun, and load var from within myfun? Thank you!

Respuesta aceptada

dpb
dpb el 24 de Feb. de 2014
A) Just put the call to load the variable in your function, perhaps with a call to uigetfile to enable the user to select the file interactively, or
B) Put the filename in the argument list instead of var and return var (or whatever is the desired result instead of having it in the argument list --
function var_perhaps_modified=myfun(inputfilename)
....
or,
C) Use nested functions and then the copy of var can be shared.
  2 comentarios
Yaser Khojah
Yaser Khojah el 20 de Jul. de 2018
Can you show please how the function var_perhaps_modified=myfun(inputfilename) works?
dpb
dpb el 20 de Jul. de 2018
It's just a prototype for whatever the OP has that would accept the filename as a string variable instead of passing the array.
In the end, however, memory usage will probably be the same either way; ML will only "copy on write" if the argument is modified and if the resulting array is modified inside the function there will need be a copy for the target, anyways...

Iniciar sesión para comentar.

Más respuestas (1)

Bernhard
Bernhard el 25 de Feb. de 2014
Editada: Bernhard el 25 de Feb. de 2014
Hi dpb,
thank you for answering to my question.
The problem with just loading var from within myfun is that var needs not to have a unique name in myfun.mat.
My idea is that the user on calling myfun either passes a variable var directly from the workspace, or the name of a variable to be loaded from a matfile, together with the name of this matfile. Starting from your suggestion I have now found a way to do this using a cell array of strings {'myfile.mat','varname'}:
function varout = myfun(varin)
if iscell(varin)
if length(varin) == 2 && ischar(varin{1}) && ischar(varin{2})
filename = varin{1};
varname = varin{2};
load(filename,varname)
var = eval(varname);
clear(varname)
else
error('wrong format of input argument')
end
else
var = varin;
end
varout = do_some_calculations(var);
end
Renaming and then clearing the loaded copy of var ensures that at the time of the calculations only one copy of var resides in the memory. This is certainly not the most elegant way to do this but it works.
Bernhard
  1 comentario
Stephen23
Stephen23 el 20 de Jul. de 2018
A much better solution would be to load into an output variable and then simply access its field:
S = load(filename,varname);
var = S.(varname);

Iniciar sesión para comentar.

Categorías

Más información sobre Workspace Variables and MAT Files en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by