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

조회 수: 9 (최근 30일)
Inti Vanmechelen
Inti Vanmechelen 2019년 4월 26일
댓글: Inti Vanmechelen 2019년 4월 29일
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!

채택된 답변

Matt J
Matt J 2019년 4월 26일
편집: Matt J 2019년 4월 26일
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
Inti Vanmechelen
Inti Vanmechelen 2019년 4월 29일
I indeed adapted my code to have all the task in one structure. Much easier, thanks for the useful advice :)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Steven Lord
Steven Lord 2019년 4월 26일
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.

Matt J
Matt J 2019년 4월 26일
편집: Matt J 2019년 4월 26일
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
Inti Vanmechelen
Inti Vanmechelen 2019년 4월 26일
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 2019년 4월 26일
편집: Matt J 2019년 4월 26일
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{:});

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by