Matlab - 从字符串定义变量

时间:2016-09-17 15:56:55

标签: string matlab output

我正在尝试制作一个Matlab脚本,将输出写入多个矩阵的* .dat文件中。矩阵“C”将转到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

我需要将输出定义为矩阵C

Output = C   % C the Matrix.

不会

Output = 'C' % Not C as a String.

但是在for循环中我只有C作为字符串

这可能是另一种方式。如果有人知道另一种更简单的方法,我也会接受其他方法。

0 个答案:

没有答案