Opening files with different names

3 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2023-6-27
编辑:Stephen23 2023-6-27
Make a list of all filenames, then use ISFILE or EXIST on them.
What have you tried so far?

请先登录,再进行评论。

采纳的回答

Ram Sreekar
Ram Sreekar 2023-6-28
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by