4

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 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

+1. Might want 'caller' instead of 'base'; then it would work when called from other functions as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.