I'm trying to make a Matlab script to write output to a *.dat files from multiple matrices. Matrix "C" will go to C.dat e.t.c.
C = rand(10,3);
R = rand(12,3);
B = rand(15,3);
W_u = rand(20,3);
W_l = rand(20,3);
names = {'C', 'R', 'B', 'W_u', 'W_l'};
for i = 1:length(names);
Output = eval(names(i)); % <- Not working as I wanted.
% I need this to read matrix C when i=1 e.t.c.
% But when using eval('C') it works for the
% first one but the rest will obviously be wrong
filename = char(strcat(names(i), '.dat')); % Creating output filename.
fid = fopen(filename, 'w'); % Opening string.
fprintf(fid, '%d\t 1\n', size(Output,1)); % Opening and writing 'header' with matrix size
fprintf(fid, '%g\t%g\t %g \n', Output.' ); % Opening and writing data.
fclose(fid); % Closing the created file.
end
I need to define the Output as the Matrix C
Output = C % C the Matrix.
NOT
Output = 'C' % Not C as a String.
But in the for loop I only have the C as a string
There is probably another ways doing this. I will also accept other methods if someone knows another easier method.