How do I make the loop run?

Hi I receive big help from Walter to read tdump file.
filename = 'tdump15120101';
fid = fopen(filename, 'rt');
tline = fgetl(fid);
numgrid = sscanf(tline, '%f', 1);
for gridnum = 1 : numgrid
tline = fgetl(fid); %read and discard
end
tline = fgetl(fid);
numtraj = sscanf(tline, '%f', 1);
for trajnum = 1 : numtraj
tline = fgetl(fid); %read and discard
end
tline = fgetl(fid);
parts = regexp( strtrim(tline), '\s+', 'split');
numvars = str2double(parts{1});
varnames = parts(2:end);
fmt = repmat('%f', 1, numvars + 12);
data = cell2mat(textscan(fid, fmt, 'CollectOutput', 1) );
fclose(fid);
traj_nums = data(:,1);
grid_nums = data(:,2);
time_of_points = datetime( [data(:,3:7), zeros(size(data,1),1)] ); %year month day hour minute stored, fill in 0 for seconds
forecast_hours = data(:,8);
traj_ages = data(:,9);
lats = data(:,10);
longs = data(:,11);
heights = data(:,12);
and I read tdump and plot to global map. Now I want to run loop because I have many tdump file. They are similar name. (ex) tdump15120101, tdump15120107, tdump15120113...) So.. could you please recommend fuction?

2 个评论

You can use the dir function to generate a list of all files, and use a for loop to loop through all files. You will have to take care not to overwrite the results from one file when processing the next. This can be as simple as storing data to a cell at the end of your loop, but there might be solutions that work better for your further processing/plotting.
I made the listing using 'dir' function. Now I don't know how read many files... I think I should use loop. but how do I use my list..?

请先登录,再进行评论。

回答(2 个)

The dir function returns a struct array, so you can loop through those:
file_list=dir('tdump*');%replace this by your own dir call
heights_cell=cell(numel(file_list),1);
for n_file=1:numel(file_list)
%filename = 'tdump15120101';
filename=file_list(n_file).name;
%rest of your code
%(make sure to store the relevant results somehow)
%(example with the heights variable:)
heights_cell{n_file}=heights;
end
List = dir(fullfile(Folder, 'tdump*'));
for iFile = 1:numel(List)
File = fullfile(folder, List(iFile).name);
% Process the file now...
end

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

提问:

2018-9-18

回答:

Jan
2018-9-19

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by