Reputation: 225
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!
Upvotes: 2
Views: 2875
Reputation: 1
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')
Upvotes: 0
Reputation: 1353
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);
Upvotes: 2
Reputation: 25160
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.
Upvotes: 4