我想根据条件(Matlab)将输入数据分配给 2 个不同的结构。做这个的最好方式是什么?
FILE points.dat
% Point ID X Y CODE
Station1 2.2 4.5 0
Station2 5.1 6.7 0
Station3 7.3 3.2 1
Station4 2.1 5.0 1
目标:如果code = 0,分配给结构A。如果不是,分配给结构B。
这是我尝试过的。真的只是在黑暗中开枪。
fid = fopen('points.dat');
C = textscan(fid, '%s %f %f %f', 'CommentStyle','%');
fclose(fid);
if (C{4} == 0)
A = struct('id',C{1}, 'x', num2cell(C{2}), 'y', ...
num2cell(C{3}), 'code', num2cell(C{4}));
else
B = struct('id',C{1}, 'x', num2cell(C{2}), 'y', ...
num2cell(C{3}), 'code', num2cell(C{4}));
end