使用Matlab保存功能 [英] Using matlabs save in functions

查看:86
本文介绍了使用Matlab保存功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在函数内使用Matlab save命令存储工作区变量?

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

请考虑以下情形:我在Matlab工作区中有很多变量,并希望所有在.mat文件中以"a"和"b"开头的变量.当然可以:

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*')

不起作用,因为save_with_name看不到工作区变量.有没有我可以使用的解决方案?

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

推荐答案

您需要在基本工作区中评估save.

You need to evaluate save in the base workspace.

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

表达式中的双引号('')对于允许引号字符本身(')是必需的. 因此,您要查找的命令是: evalin

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

这篇关于使用Matlab保存功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆