How to bypass empty fields in structure when loading data?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ali Motahharynia
le 1 Juin 2020
Commenté : Ali Motahharynia
le 17 Juin 2020
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 commentaires
Image Analyst
le 1 Juin 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);
Réponse acceptée
Ameer Hamza
le 2 Juin 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Block Authoring Basics dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!