Using math functions to save in functions

Can I use the save Matlab command inside a function to store workspace variables?

Consider the following scenario: I have a bunch of variables in the Matlab workspace and want everything that starts with "a" and "b" in the .mat file. Of course this works:

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

but I want to have a variable file name. The function I wrote:

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

does not work because save_with_name does not see workspace variables. Is there a solution that I can use?

+4
source share
1 answer

You need to evaluate save in the underlying workspace.

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

Double quotation marks ('') in an expression are necessary to resolve the quotation mark itself ('). So the command you are looking for is: evalin

+5
source

Source: https://habr.com/ru/post/1381491/


All Articles