check if the destination file is the same as what I want

1 回表示 (過去 30 日間)
Indrani
Indrani 2023 年 6 月 21 日
編集済み: Florian Bidaud 2023 年 6 月 21 日
Hi!
I have a folder inside which there are multiple .mat files. I want to perform an operation such that I get the plots of only 2 specific .mat files. For example,
The path that contains the .mat files are -- 'C:\ParentFolder\Folder\Subfolder\*.mat'
Now I want to perform an operation like -- *.mat == a.mat.
All this will be done in a for loop as I have to do this step for many files (in different folders).
How do I proceed?

回答 (2 件)

Florian Bidaud
Florian Bidaud 2023 年 6 月 21 日
編集済み: Florian Bidaud 2023 年 6 月 21 日
I guess you can use isfile to check if the matlab file you need is somewhere in the folder.
Like
if isfile(a.mat)
my_file = a.mat
end
If you want to put all your files in an array:
file_name_array = {'file1' 'file2' 'file3'};
path = 'C:\folder'
for i = 1:length(file_name_array)
if isfile(fullfile(path,file_name_array{1}))
files.(file_name_array{1}) = load(fullfile(path,file_name_array{1}));
else
disp([file_name_array ' does not exist'])
end
end

Mayur
Mayur 2023 年 6 月 21 日
Hi Indrani!
You can use the dir function to get a list of all .mat files in the specified folder, and then loop over this list to load and plot the data from the desired files.
folder = 'C:\ParentFolder\Folder\Subfolder\';
fileList = dir(fullfile(folder, '*.mat'));
for i = 1:length(fileList)
filename = fileList(i).name;
if strcmp(filename, 'a.mat') || strcmp(filename, 'b.mat')
data = load(fullfile(folder, filename));
% Remaining code
end
end
You can read more about dir and fullfile from the following documentations:

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by