Loop in all scripts
4 views (last 30 days)
Show older comments
Hi all,
I have a GUI that needs to loop through a certain amount of files (depends on the user selection). The files are loaded with this code:
FNM = handles.FNM;
Filenames = handles.Filenames;
for k = 1:length(FNM)
current = FNM {k};
kstr = num2str(k);
amountFilesstr = num2str(length(FNM));
baseFileName = Filenames(k).name;
fullfilename = fullfile(foldername, baseFileName);
Currentfiletest = ['Now loading file ' kstr ' of ' amountFilesstr ' ' '(' current ')'];
data = readtable(fullfilename);
set(handles.figure1, 'pointer', 'watch');
drawnow;
set(handles.figure1, 'pointer', oldpointer);
set(handles.text7, 'string', Currentfiletest);
drawnow
handles.data = data;
complete = ['Files have been loaded'];
set(handles.text7, 'string', complete);
end
This is done in pushbutton1
Now, pushbutton 2 should do the analyses on all these files. I am using the following code:
if get(handles.specfiles, 'value') == 1
FNM = handles.FNM;
for i = 1:length(FNM)
ReadData
WorkTimeSpec
AwakeTimeSpec
NonWearAwakeSpec
NonWearWorkSpec
assignin('base', 'Data', Data)
if get(handles.diaryawake, 'value') == 0 && get(handles.checkbox12, 'value') == 0 &&...
get(handles.checkbox10, 'value') == 0 && get(handles.validdays, 'value') == 0 &&...
get(handles.checkbox31, 'value') == 0
Data = TimevCounts;
end
NonWearTime
ValidWearTime
SplitValidData
CutoffPoint
Bouts
Breaks
end
I am new to matlab and have no experience with for loops but I thought this is the way to do it. However, the data does not add up and I cannot seem to figure out why..
Hope anyone can help me
0 Comments
Accepted Answer
Stephen23
on 30 May 2018
Edited: Stephen23
on 30 May 2018
This line of code inside the loop:
handles.data = data;
completely replaces the field data on each loop iteration: on the second iteration it completely replaces whatever was allocated during the first iteration, on the third it replaces what was allocated on the second, etc. So at the end you only have the last iterations's data. If you want to save the data from each iteration, then do something like this:
handles.data = cell(1,numel(FNM));
for k = 1:numel(FNM)
...
handles.data{k} = data;
...
end
See also:
2 Comments
Stephen23
on 31 May 2018
Edited: Stephen23
on 31 May 2018
"When I run the script, it gives me an error in the following script that uses data:"
You get an error because now the field data is a cell array of size 1xN containing the data for the N imported files, and yet in your code you try to access data like this:
startdate = data(3,:);
Does a 1xN cell array have a third row? No. You will either have to access each cell separately (e.g. in a loop, just like how the files were imported), or concatenate all of the imported data into one numeric arrays (this requires them to be of a suitable size). Which one you do depends on your algorithm (i.e. how you want to process your data).
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!