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

1 Ansicht (letzte 30 Tage)
Indrani
Indrani am 21 Jun. 2023
Bearbeitet: Florian Bidaud am 21 Jun. 2023
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?

Antworten (2)

Florian Bidaud
Florian Bidaud am 21 Jun. 2023
Bearbeitet: Florian Bidaud am 21 Jun. 2023
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 am 21 Jun. 2023
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:

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by