How to ignore non-existing variables in a for loop?
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Inti Vanmechelen
il 26 Apr 2019
Commentato: Inti Vanmechelen
il 29 Apr 2019
Hi,
I am trying to load different structures with a for loop. However, some of the structures will not contain the field that is used in the for loop.
I think I need the 'if', 'continue', 'end' statement, but I'm struggling with how to define the 'if'.
MOVEMENT = {'RF1','RF2','RF3','RGV1','RGV2','RGV3','RS1','RS2','RS3'};
'MOVEMENT' contains the tasks I want to select, but some of the structures I load have e.g. RGV1 and RGV3 but not RGV2.
I tried:
if exist(MOVEMENT{f}, 'var') == 0
end
continue
But this does not seem to work.
Code looks like this currently:
for f = 1:length(MOVEMENT)
% File does not exist
% Skip to bottom of loop and continue with the loop
if exist(MOVEMENT{f}, 'var') == 0
H18.(MOVEMENT{f}) = load(['D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\H18\Mean_Subject_' ...
MOVEMENT{f} '.mat']);
H10.(MOVEMENT{f}) = load(['D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\H10\Mean_Subject_' ...
MOVEMENT{f} '.mat']);
H13.(MOVEMENT{f}) = load(['D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\H13\Mean_Subject_' ...
MOVEMENT{f} '.mat']);
end
continue
end
Any suggestion is greatly appreciated!
0 Commenti
Risposta accettata
Matt J
il 26 Apr 2019
Modificato: Matt J
il 26 Apr 2019
The code you've posted indicates that your "variables" reside in separate .mat files, like
Mean_Subject_RF1.mat
Mean_Subject_RGV2.mat
The whole thing would be much easier if you re-organized how you store things so that the variables present for Hxx all reside in the same .mat file:
pth='D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\';
save( fullfile(pth,'H18\MeanSubject.mat') , 'RF1','RGV2',...)
Then instead of loading the variables one-by-one, do
H18=load(fullfile(pth,'H18\MeanSubject.mat'));
This gives you, in a single command, a struct H18 containing all the variables (and only those variables) that were present for H18.
Più risposte (2)
Steven Lord
il 26 Apr 2019
I interpreted your question a bit differently than I think Matt J did. I suspect you want to load your data then check if the desired field is present in the loaded data. If so use the isfield function to check for the presence of the field in the data.
If instead you want to skip loading the file entirely if a particular variable is not present in the file, you can test this using the whos function with the -file option.
0 Commenti
Matt J
il 26 Apr 2019
Modificato: Matt J
il 26 Apr 2019
if ismember(varname, MOVEMENT)
H18.(varname) = load(['D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\H18\Mean_Subject_' ...
varname '.mat']);
H10.(varname) = load(['D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\H10\Mean_Subject_' ...
varname '.mat']);
H13.(varname) = load(['D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\H13\Mean_Subject_' ...
varanme '.mat']);
end
2 Commenti
Matt J
il 26 Apr 2019
Modificato: Matt J
il 26 Apr 2019
Sorry. I think this might be what you want:
Paths={'D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\H18';
'D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\H10';
'D:\Toolkit\Backup\CRD-L-06022\c\Users\u0102306\Box Sync\DATA\PROCESSED_DATA\IDCA\IMU\H13'};
clear Hcell
for p = 1:length(Paths)
for f=1:length(MOVEMENT)
varname=['Mean_Subject_' MOVEMENT{f}];
filename=fullfile(Paths{p},varname,'.mat');
if exist(fullfile, 'file')
Hcell{p}.(varname) = load(filename);
end
end
end
[H18,H10,H13]=deal(Hcell{:});
Vedere anche
Categorie
Scopri di più su Variables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!