How to open files with similar names
4 views (last 30 days)
Show older comments
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/243366/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!
:)
0 Comments
Answers (1)
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
0 Comments
See Also
Categories
Find more on Testing Frameworks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!