How to bypass empty fields in structure when loading data?

1 Ansicht (letzte 30 Tage)
My experiment contains my subjects data and each subject consists of different blocks before I do the analysis I merged my data into one mat file, but some of my subjects have missing blocks, so I bypassed those data and merge the file. However the problem is missing blocks represent like this [ ], since some blocks are like this [ ], when I want to load different field of my structure I get this error '' Dot indexing is not supported for variables of this type '' Therefore I need to bypass these blocks in the loop, In this regard I added this code
if isempty(all_Subjects(block_Counter, subject_Counter).all_results) == 0
% do the workk
else
break
end
but I got empty results for saving my data in table
% merging Information of different Subjects
Subjects_ID = [];
Blocks_ID = [];
reactionTime_of_all_Subjects = [];
performance_of_all_Subjects = [];
for subject_Counter = s_Initial:s_Final
% select data with fields
if isempty(all_Subjects(block_Counter, subject_Counter).all_results) == 0
% merging Information of different blocks
all_reactionTime_of_one_Subject = [];
all_subject_Performance_of_one_Subject = [];
for block_Counter = B_Initial:B_Final
reactionTime = all_Subjects(block_Counter, subject_Counter).all_results.reactionTime;
subject_Performance = all_Subjects(block_Counter, subject_Counter).all_results.performance;
% merging Information of different blocks
all_reactionTime_of_one_Subject = [all_reactionTime_of_one_Subject; reactionTime];
all_subject_Performance_of_one_Subject = [all_subject_Performance_of_one_Subject; subject_Performance];
% Generate label for Subjects
for i = 1: length(reactionTime)
Subjects_ID = [Subjects_ID; subject_Counter]
Blocks_ID = [Blocks_ID; block_Counter]
end
end
% merging Information of different Subjects
performance_of_all_Subjects = [performance_of_all_Subjects; all_subject_Performance_of_one_Subject];
reactionTime_of_all_Subjects = [reactionTime_of_all_Subjects; all_reactionTime_of_one_Subject];
else
break
end
end
% Mering all data to table
analysis = array2table([Subjects_ID, Blocks_ID, reactionTime_of_all_Subjects, performance_of_all_Subjects]);
analysis.Properties.VariableNames{1} = 'Subjects_ID';
analysis.Properties.VariableNames{2} = 'Block_ID';
analysis.Properties.VariableNames{3} = 'ReactionTime';
analysis.Properties.VariableNames{4} = 'Performance';
save('data_for_Analysis','analysis')
  5 Kommentare
Image Analyst
Image Analyst am 1 Jun. 2020
This code ran fine with no errors, with essentially no change to your code.
fprintf('Beginning to run %s.m.\n', mfilename);
s = load('all_subjects.mat')
all_Subjects = s.all_Subjects
% Merging subjects data
s_Initial = 1;
s_Final = 3;
B_Initial = 1;
B_Final = 2;
Subjects_ID = [];
Blocks_ID = [];
% merging Information of different Subjects
Subjects_ID = [];
Blocks_ID = [];
reactionTime_of_all_Subjects = [];
performance_of_all_Subjects = [];
for subject_Counter = s_Initial:s_Final
% select data with fields
% if isempty(all_Subjects(block_Counter, subject_Counter).all_results) == 0
% merging Information of different blocks
all_reactionTime_of_one_Subject = [];
all_subject_Performance_of_one_Subject = [];
for block_Counter = B_Initial:B_Final
reactionTime = all_Subjects(block_Counter, subject_Counter).all_results.reactionTime;
subject_Performance = all_Subjects(block_Counter, subject_Counter).all_results.performance;
% merging Information of different blocks
all_reactionTime_of_one_Subject = [all_reactionTime_of_one_Subject; reactionTime];
all_subject_Performance_of_one_Subject = [all_subject_Performance_of_one_Subject; subject_Performance];
% Generate label for Subjects
for i = 1: length(reactionTime)
Subjects_ID = [Subjects_ID; subject_Counter]
Blocks_ID = [Blocks_ID; block_Counter]
end
end
% merging Information of different Subjects
reactionTime_of_all_Subjects = [reactionTime_of_all_Subjects; all_reactionTime_of_one_Subject];
performance_of_all_Subjects = [performance_of_all_Subjects; all_subject_Performance_of_one_Subject];
% else
% break
% end
end
analysis = array2table([Subjects_ID, Blocks_ID, reactionTime_of_all_Subjects, performance_of_all_Subjects]);
analysis.Properties.VariableNames{1} = 'Subjects_ID';
analysis.Properties.VariableNames{2} = 'Block_ID';
analysis.Properties.VariableNames{3} = 'ReactionTime';
analysis.Properties.VariableNames{4} = 'Performance';
save('data_for_Analysis.mat','analysis')
fprintf('Done Running %s.m.\n', mfilename);
Ali Motahharynia
Ali Motahharynia am 1 Jun. 2020
thank you for that, and sorry I made a little mistake in sendining the code for the second time, if we put B_Final value to 3 we got the error and that's my problem, the all_Subjects results contain a 3*3 structure, but the row 3 of the structure have 2 missing blocks(1 and 3), and only the second has value, so when the code want to read the file it crashes and say ''Dot indexing is not supported for variables of this type.
Error in merger (line 25)
reactionTime = all_Subjects(block_Counter,
subject_Counter).all_results.reactionTime;''

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 2 Jun. 2020
Use isstruct(): https://www.mathworks.com/help/releases/R2020a/matlab/ref/isstruct.html. For example, use the following if condition to only access data of it is a struct
if isstruct(all_Subjects(block_Counter, subject_Counter).all_results)
reactionTime = all_Subjects(block_Counter, subject_Counter).all_results.reactionTime;
subject_Performance = all_Subjects(block_Counter, subject_Counter).all_results.performance;
else
reactionTime = 0; % or something else
subject_Performance = 0;
end
  1 Kommentar
Ali Motahharynia
Ali Motahharynia am 17 Jun. 2020
thanks for your answer both isempty and isstruct work very well but my mistake is that I put ''if'' in wrong place and thanks to you I figured that out, moreover if I put break after else it will be better and it won't add one 0 to the end of my data.
thank you

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by