MATLAB Answers

How to ignore non-existing variables in a for loop?

23 views (last 30 days)
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 Comments

Sign in to comment.

Accepted Answer

Matt J
Matt J on 26 Apr 2019
Edited: Matt J on 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.

  1 Comment

Inti Vanmechelen
Inti Vanmechelen on 29 Apr 2019
I indeed adapted my code to have all the task in one structure. Much easier, thanks for the useful advice :)

Sign in to comment.

More Answers (2)

Steven Lord
Steven Lord on 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 Comments

Sign in to comment.


Matt J
Matt J on 26 Apr 2019
Edited: Matt J on 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 Comments

Inti Vanmechelen
Inti Vanmechelen on 26 Apr 2019
Thank you Matt!
Not sure if I'm being stupid or just not getting but I think I'm missing another step.
I implemented your suggestion like this:
for f = 1:length(MOVEMENT)
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_' ...
varname '.mat']);
end
continue
end
But from what I understand from the ismember help page, it now compares 'varnames' and 'MOVEMENT' for equal content. But then I'd need to define 'varname' first?
Now it gives me the error "Undefined function or variable 'varname'."
Thanks in advance :)
Matt J
Matt J on 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{:});

Sign in to comment.

Tags