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

1 次查看(过去 30 天)
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