passing a mat file variable to a function
6 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
dpb
2014-2-24
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 个评论
Yaser Khojah
2018-7-20
Can you show please how the function var_perhaps_modified=myfun(inputfilename) works?
dpb
2018-7-20
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...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!