Loop in all scripts

4 ビュー (過去 30 日間)
Debbie Oomen
Debbie Oomen 2018 年 5 月 30 日
編集済み: Stephen23 2018 年 5 月 31 日
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

採用された回答

Stephen23
Stephen23 2018 年 5 月 30 日
編集済み: Stephen23 2018 年 5 月 30 日
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 件のコメント
Debbie Oomen
Debbie Oomen 2018 年 5 月 30 日
Hi Stephen, Thanks for your fast response. Now I have put this in the pushbutton1 command:
elseif get(handles.specfiles, 'value') == 1
FNM = handles.FNM;
handles.data = cell(1,numel(FNM))
Filenames = handles.Filenames;
for k = 1:numel(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{k} = data;
complete = ['Files have been loaded'];
set(handles.text7, 'string', complete);
end
I am not sure if this is what you meant? When I run the script, it gives me an error in the following script that uses data:
data = handles.data;
startdate = data(3,:);
startdate = table2cell(startdate);
startdate = startdate {:}; %converts to character
startdate = strsplit(startdate);
startdate = startdate{:,3};
startdatevec = datevec(startdate, 'dd/mm/yyyy');
So I don't think that I have placed this correctly. I also havent changed anything in the pushbutton2 script
Stephen23
Stephen23 2018 年 5 月 31 日
編集済み: Stephen23 2018 年 5 月 31 日
"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).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by