Opening files with different names

4 visualizzazioni (ultimi 30 giorni)
Bence Laczó
Bence Laczó il 27 Giu 2023
Risposto: Ram Sreekar il 28 Giu 2023
I am using Matlab to read mat files generated during data collection.
The files have the following format: they start with either anterior, central, posterior, lateral or medial and end with a number (-10,-09,-08,-07,-06,-05,-04,-03,-02,-01,+00,+01,+02,+03,+04,+05).
Example: anterior-10.mat, central+02.mat
After analysis of the data I have mad for example "times_anterior-10.mat" file. These files however doesn't exist for every raw file.
I have two different scripts which I would like to run to further analise the data. I would like to run the 1st script if "times_*.mat" file doesn't exist and I would like to run the 2nd script if "times_*.mat" file exist.
  1 Commento
Stephen23
Stephen23 il 27 Giu 2023
Modificato: Stephen23 il 27 Giu 2023
Make a list of all filenames, then use ISFILE or EXIST on them.
What have you tried so far?

Accedi per commentare.

Risposta accettata

Ram Sreekar
Ram Sreekar il 28 Giu 2023
You can run different scripts based on the existence of the "times_*.mat" file by using the ‘exist’ function in MATLAB. You can refer to the example code given below.
% Check if the "times_*.mat" file exists
if exist('times_*.mat', 'file') == 2
% Run the 2nd script if the file exists
run('second_script.m');
else
% Run the 1st script if the file doesn't exist
run('first_script.m');
end
In this code, the ‘exist’ function is used to check if the file "times_*.mat" exists in the current directory. The function returns 2 if the file exists, and 0 otherwise. Based on this result, the appropriate script is executed using the run function.
Please make sure to replace "times_*.mat" with the actual file name required and the script names to the required scripts that you wish to run.
Hope this helps you resolve your query.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by