How to open files with similar names

4 ビュー (過去 30 日間)
Carlo Speranza
Carlo Speranza 2019 年 10 月 18 日
回答済み: Robert U 2019 年 10 月 18 日
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!
:)

回答 (1 件)

Robert U
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

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by