How to bypass empty fields in structure when loading data?
Show older comments
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 Comments
Image Analyst
on 1 Jun 2020
Please attach all_Subjects:
save('answers.dat', 'all_Subjects');
with the paper clip icon so we can run your code.
Stephen23
on 1 Jun 2020
@Ali Motahharynia: please show the complete error message. This means all of the red text.
Ali Motahharynia
on 1 Jun 2020
Edited: Ali Motahharynia
on 1 Jun 2020
Image Analyst
on 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
on 1 Jun 2020
Accepted Answer
More Answers (0)
Categories
Find more on General Applications 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!