如何在文件中写入不同长度的向量?

时间:2018-11-02 10:35:47

标签: matlab vector printf

我有7个长度不同的向量(L_mean_argil,L_Min_argil,L_Max_argil,R_mean_argil,R_Min_argil,R_Max_argil,Zero_argil),我想创建一个包含所有向量的.sae文件。这是我的代码:

fileID = fopen('BN_for_prediciting_zeros_argilliti_v1.sae','w');
fprintf(fileID,'L_mean_argil,   L_Min_argil,    L_Max_argil,    R_mean_argil,   R_Min_argil,    R_Max_argil, Zero_argil\n');
fprintf(fileID,'%6.4f, %6.4f, %6.4f, %6.4f, %6.4f, %6.4f, %6.4f \n', L_mean_argil,  L_Min_argil,    L_Max_argil,    R_mean_argil,   R_Min_argil,    R_Max_argil, Zero_argil');
fclose(fileID);

它不能正确写入向量。有人可以帮助我吗?

我希望这样:

L_mean_argil(1)   L_Min_argil(1)   ...  Zero_argil(1)
L_mean_argil(2)   L_Min_argil(2)   ...  Zero_argil(2)
...
L_mean_argil(end) L_Min_argil(end) ...  Zero_argil(end)    

但是在某些向量的最后部分,将存在与其他向量相关联的空单元格

1 个答案:

答案 0 :(得分:1)

这是一个假设您要在缺少数据的行中留空的答案。

%Sample Data all of different lengths
a = 1:4;
b = (1:6)+1;
c = (1:5)+2;

%Get max length
maxSize = max([numel(a) numel(b) numel(c)]);

%Convert to Cell array if it isn't already
a = num2cell(a,1);
b = num2cell(b,1);
c = num2cell(c,1);

%Initialize a cell to hold everything. Init to the max size
output = cell(3,maxSize);
output(1,1:numel(a)) = a;
output(2,1:numel(b)) = b;
output(3,1:numel(c)) = c;

%Print...
fprintf('%0.1f,%0.1f,%0.1f\n',output{:})

结果如下:

1.0,2.0,3.0
2.0,3.0,4.0
3.0,4.0,5.0
4.0,5.0,6.0
,6.0,7.0
,7.0,