MATLAB: Passing a mat file variable to a function

.mat filefunctionlarge variableload

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!

Best Answer

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.