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 CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by