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 个评论
Rik
2018-9-18
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.
SONGYI KIM
2018-9-19
回答(2 个)
Rik
2018-9-19
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
Jan
2018-9-19
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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!