How to open files with similar names
3 次查看(过去 30 天)
显示 更早的评论
Hi everybody,
I need to open in Matlab 3 files with similar names. Every file contains pressure coefficients during an experiment with wind flow. Every experiment is done three times, for wind angle of 90°, 60°, 30°. I want to write a code that, when I select one of the three files, it automatically loads also the other two files of the experiment.
So, when I select the file with "Ang+090", the code should load also the files with "Ang+060" and "Ang+030".
This is my folder:
The code that I use to open a single file is:
[SIW pathnameSIW]=uigetfile('C:\Users\directory\*.*','Choose the file you want to open');
eval(['load ' , pathnameSIW SIW]);
Thanks very much in advance!
:)
0 个评论
回答(1 个)
Robert U
2019-10-18
Hi Carlo Speranza,
first of all, get rid of the eval()-command. That is usually not needed, difficult to debug and prone to errors.
One possible way to get similar filenames is to filter utilizing regexp. An example that you can alter to fit your needs you find below:
% test names you would get with dir()-command and fileparts()-command
filenames = {'045_SIW_ETR1000_BS_H1_TL_P8500_Ang+090',...
'046_SIW_ETR1000_BS_H1_TL_P8500_Ang+060',...
'049_SIW_ETR1000_BS_H1_TL_P8500_Ang+030',...
'051_SIW_ETR1000_BS_H1P5_UP_TL_P8500_Ang+090',...
'053_SIW_ETR1000_BS_H1P5_UP_TL_P8500_Ang+090',...
'055_SIW_ETR1000_BS_H1P5_UP_TL_P8500_Ang+090'};
% input from uigetdir and fileparts
fileToLoad = '045_SIW_ETR1000_BS_H1_TL_P8500_Ang+090';
% extract blueprint of name
blueprint = regexp(fileToLoad,'(?<=^\d+)_.+(?=+\d+)','match');
% filter names for similarity
simFileNames = filenames(~cellfun(@isempty,regexp(filenames,sprintf('(?<=^\\d+)%s(?=+\\d+)',blueprint{1}))));
for ik = 1:numel(simFileNames)
DataIn(ik) = load([simFileNames{ik},'.mat']);
end
Kind regards,
Robert
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!