How to bypass a directory that is not exist?

조회 수: 4 (최근 30일)
Ali Motahharynia
Ali Motahharynia 2020년 5월 31일
댓글: Ali Motahharynia 2020년 6월 1일
My experiment contains my subjects data and each subject consists of different blocks, before I do the analysis I need to merge my data into one mat file, so first I need to load all of the data, but the problem is some of my subjects have missing blocks and when my codes want to read all data, It crashes and said ''Unable to read file 'Subject 1 Block 3.mat'. No such file or directory'' , so I need to bypass the data that their directory doesn't exist, so I wrote this if statement inside my code but it didn't work:
%load data
s_Initial = 1;
s_Final = 100;
B_Initial = 1;
B_Final = 3;
% merging Data
Subjects_ID = [];
Block_ID = [];
% merge Data
for subject_Counter = s_Initial: s_Final
for block_Counter = B_Initial: B_Final
% load the directory name
load_Data = fullfile(append('Subject', ' ',num2str(subject_Counter),...
' ', 'Block',' ',num2str(block_Counter),'.mat'));
if class(load_Data) == 'char'
all_Subjects(block_Counter, subject_Counter) = load(load_Data);
else
break ;
end
Subjects_ID = [Subjects_ID; subject_Counter];
Block_ID = [Block_ID; block_Counter];
end
end
save('all_Subjects')

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 31일
편집: Ameer Hamza 2020년 5월 31일
See exist() function https://www.mathworks.com/help/releases/R2020a/matlab/ref/exist.html. Call it with with search type 'file'
if exist(load_Data, 'file')
  댓글 수: 1
Ali Motahharynia
Ali Motahharynia 2020년 6월 1일
Thanks for your answer, this works really nice.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 5월 31일
Try isfile():
%load data
s_Initial = 1;
s_Final = 100;
B_Initial = 1;
B_Final = 3;
% merging Data
Subjects_ID = [];
Block_ID = [];
% merge Data
for subject_Counter = s_Initial: s_Final
for block_Counter = B_Initial: B_Final
% load the directory name
load_Data = fullfile(append('Subject', ' ',num2str(subject_Counter),...
' ', 'Block',' ',num2str(block_Counter),'.mat'));
if isfile(load_Data)
% If the file actually exists.
all_Subjects(block_Counter, subject_Counter) = load(load_Data);
Subjects_ID = [Subjects_ID; subject_Counter];
Block_ID = [Block_ID; block_Counter];
end
end
end
save('all_Subjects')
  댓글 수: 3
Image Analyst
Image Analyst 2020년 6월 1일
isfile() is the newer, preferred way of doing it. You can only accept one answer however you can "Vote" for any number of answers to award the answerer "reputation points." Thanks in advance. 😊
Ali Motahharynia
Ali Motahharynia 2020년 6월 1일
Thank you for informing me, and I already voted for your nice answer,
I also have another problem for the next lines of this code and I already asked that in mathwork, I will appreciate it if you take a look at that at this link, Thank you in advance. 😊

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by