Creating for loop with an if statement

6 views (last 30 days)
Bruce Lee
Bruce Lee on 2 Aug 2023
Answered: dpb on 2 Aug 2023
Hi, I have 100 data files, 50 of the files are of one set named interictal going from 1-50 and the other 50 named ictal also going from 1-50. I am trying to load the first 50 of interictal then 50 of the ictal after. I am trying to load both in one for loop and am struggling to do so. With my code so far I am able to load all 50 of the interictal but after that it loads one ictal then tries to load interictal 51 which it cannot as it doesnt exist. any help would be much appreciated.
s=100
s = 100
H=zeros(1,s)
H = 1×100
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
for segment=1:s
if segment==50
filename=['MATLAB/matlab 2023/data/Patient_5_ictal_segment_',num2str(segment),'.mat']
load(filename)
end
filename=['MATLAB/matlab 2023/data/Patient_5_interictal_segment_',num2str(segment),'.mat']
load(filename)
end
filename = 'MATLAB/matlab 2023/data/Patient_5_interictal_segment_1.mat'
Error using load
Unable to find file or directory 'MATLAB/matlab 2023/data/Patient_5_interictal_segment_1.mat'.

Answers (1)

dpb
dpb on 2 Aug 2023
I'd go at this differently...
ROOTDATADIR='MATLAB/matlab 2023/data/'; % separate out the data from the code
PATIENTFILE='Patient_5'; % select the particular patient wanted
d=dir(fullfile(ROOTDATADIR,PATIENTFILE,'*ictal*.mat')); % get the list of both _ictal & _interictal files
for i=1:numel(d) % and walk through it...
load(fullfile(d(i).folder,d(i).Name)) % then load each in turn...
.... do whatever with each here ... can include test for whether is _inter or not, etc., etc., ...
end

Categories

Find more on App Building in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!