How to select and loop variables in a filename
1 次查看(过去 30 天)
显示 更早的评论
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
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?
回答(1 个)
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Weather and Atmospheric Science 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!