How to select and loop variables in a filename

조회 수: 1 (최근 30일)
Sammy
Sammy 2022년 8월 2일
댓글: Sammy 2023년 6월 18일
I have monthly files with 2 variables: sst (jan_sst03) and time(jan_dt03) per file, how do I loop all my files such that it will take the mean of the sst variable only?
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 8월 2일
편집: Walter Roberson 2022년 8월 2일
Are those mat files? xlsx?
Does the name of the variable change in each file? So the substring "sst" needs to be checked for?
Sammy
Sammy 2023년 6월 15일
편집: Sammy 2023년 6월 18일
I figured this one out already. By making a timeseries of the files (jan2003-dec2022) (the names of the variable does not change). Then, using the time variable to find the duration or period to average the sst variable by, e.g. idx=find(mo==1), I can now average all the january months, making my january monthly climatology. (:

댓글을 달려면 로그인하십시오.

답변 (1개)

Shree Harsha Kodi
Shree Harsha Kodi 2023년 6월 17일
Folder = 'path_to_folder'; % Specify the folder containing your files
files = dir(fullfile(folder, '*.mat')); % Get a list of all .mat files in the folder
means = [];
for i = 1:numel(files)
data = load(fullfile(folder, files(i).name)); % Load the data from the file
sst = data.sst; % Extract the "sst" variable from the loaded data
mean_sst = mean(sst); % Calculate the mean of the "sst" variable
means = [means mean_sst]; % Store the mean value in the array
end
% Display the mean values
disp(means);
  댓글 수: 1
Sammy
Sammy 2023년 6월 18일
Hi Shree! I figured this one out a year ago when I made monthly climatological means. But thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Weather and Atmospheric Science에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by