2

I want to load multiple variables into one .mat file at the end of a process loop. I have a simple line of code :

save draw.mat Output

but I cannot work out a way to code 'use the name given by variable X' instead of 'Output', so that I can loop the process and save multiple variables in draw.mat

So then

X = 'Chocolate'

and the Variable name is saved as Chocolate.

I am sure it is simple but I cannot find a solution on here!

3 Answers 3

4

You need the functional form of SAVE. In other words, SAVE can be called like this:

save('draw.mat', 'Output1', 'Output2');

So, if your variable names to save are in a separate variable, you could do

v1 = 'Output1';
v2 = 'Output2';
save('draw.mat', v1, v2);

Or even

v = {'Output1', 'Output2'};
save('draw.mat', v{:});

The SAVE reference page has full details.

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

1 Comment

And also: S = 'draw.mat'; save(S, v1, v2)
2

You can use the -struct form of the save command. You build a struct with fields holding the names of the variables in the resulting .mat-file.

Example:

s = struct();
s.VariableOne = 1;
s.VariableTwo = 2;
save draw.mat -struct s;

The file draw.mat will now hold two 1x1 double variables with names "VariableOne" and "VariableTwo".

You can also build the struct in one single command:

s = struct('VariableOne', {1}, 'VariableTwo', {2});

Or you can use the cell2struct function:

data = {1,2};
names = {'VariableOne', 'VariableTwo'};
s = cell2struct(data(:), names(:), 1);

Comments

0

Let

A = [2 5 8; 25 2 4; 4 1 7];
save('A.mat')

Now you want to save it with other name say B

B = A;
save('B.mat')

Comments

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.