Using matlabs save in functions

北城余情 提交于 2019-12-23 12:17:44

问题


Is it possible to use the Matlab save command inside a function to store workspace variables?

Consider following scenario: I've got a bunch of variables in the Matlab workspace and want all that are beginning with "a" and "b" in a .mat file. Of course this works:

save('test.mat','a*','b*')

but i want to have a variable filename. The function i wrote:

function save_with_name(name)
save(name,'a*','b*')

does not work, because save_with_name doesn't see the workspace variables. Is there a solution which i can use?


回答1:


You need to evaluate save in the base workspace.

function save_with_name(name)
expression = ['save(''', name, ''',''a*'',''b*'')'];
evalin('base',expression);

The double-quotes ('') in the expression are necessary to allow the quote character itself ('). Thus the command you're looking for is: evalin



来源:https://stackoverflow.com/questions/8152740/using-matlabs-save-in-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!