Matlab:顺序文件循环

时间:2014-05-28 15:11:38

标签: matlab loops import sequence

我想循环以下脚本,以便00001将按顺序1到100顺序增加(到00002,00003等).00001出现3次: 在%% Initialize variables下: r5004b_00001.dat ,在%% Allocate imported array to column variable names下: Angle00001 Intensity00001

%% Initialize variables.
filename = sprintf('E:\XRD\Enamel\r5004b_00001.dat');
startRow = 5;

%% Format string for each line of text:
formatSpec = '%14f%f%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

%% Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines' ,startRow-1, 'ReturnOnError', false);

%% Close the text file.
fclose(fileID);

%% Allocate imported array to column variable names
Angle00001 = dataArray{:, 1};
Intensity00001 = dataArray{:, 2};

%% Clear temporary variables
clearvars filename startRow formatSpec fileID dataArray ans;

1 个答案:

答案 0 :(得分:2)

对于文件名,这是一个好主意,这是解决方案(我只使用串联来表示清晰度,sprintf当然足够了):

number = 3;
s = sprintf('%05d', number); % will produce '00003'
filename = ['E:\XRD\Enamel\r5004b_'  s '.dat'];

对于变量do not do this。最好使用数组,或者,如果你真的喜欢有很多名字,那么结构应该是动态字段名称:

strct.(['angle' s]) = ...

如果你真的想达到你的要求,可以用

来完成
eval(['a' s ' = 1 + 1;'])