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!