How to open files with similar names

7 views (last 30 days)
Carlo Speranza
Carlo Speranza on 18 Oct 2019
Answered: Robert U on 18 Oct 2019
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:
Cattura.PNG
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!
:)

Answers (1)

Robert U
Robert U on 18 Oct 2019
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

Community Treasure Hunt

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

Start Hunting!