Error when trying to use if exist command. variable exists but it is not recognising it
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi
I'm trying to run a section of code that find's variables I've imported from a c3d file. Unfortunately from file to file the location of the variable changes.
Therefore the code I'm running is designed to search if the variable I want exists under on sturcture. If variable doesn't exist then search for it under an alternative location.
With some files I'm getting the error "Reference to non-existent field"
Can anyone Identify where my error in my code might be? In the instance that I'm getting this error the marker.LkneeMoments does exist but it's skipping past that straight to the moment.LKneeMoments which doesn't exist (hence the error message). Changing to use elseif instead of if doesn't work either.
if exist('markers.LKneeMoments','var');
WAPhaseMoments.KneeMoment = markers.LKneeMoments(WAStartFrame:WAEndFrame,:);
fprintf('\t\t\t Imported WA Knee Moments\n')
else exist('moments.LKneeMoments','var');
WAPhaseMoments.KneeMoment = moments.LKneeMoments(WAStartFrame:WAEndFrame,:);
fprintf('\t\t\t Imported WA Knee Moments\n')
end
Any suggestions are more than welcome
0 Kommentare
Antworten (1)
Guillaume
am 4 Mai 2017
Bearbeitet: Guillaume
am 4 Mai 2017
Notwithstanding the syntax error in your else branch, the problem you have is that you're not passing a variable name to exist. The variables are either markers or moments. The .LKneeMoments is not part of the variable names. It's a field of the variables and you test its existence with isfield.
So, if you're sure that both variables markers and moments exist (you should be!) and you're simply unsure whether or not they have the required field your code should be:
if isfield(markers, 'LKneeMoments') %semicolon should not be there
WAPhaseMoments.KneeMoment = markers.LKneeMoments(WAStartFrame:WAEndFrame,:);
fprintf('\t\t\t Imported WA Knee Moments\n')
elseif isfield(moments, 'LKneeMoments')
WAPhaseMoments.KneeMoment = moments.LKneeMoments(WAStartFrame:WAEndFrame,:);
fprintf('\t\t\t Imported WA Knee Moments\n')
end
If you're not sure that either markers or moments exist then, a) how come? and b) combine exist and isfield:
if exist('markers', 'var') && isfield(markers, 'LKneeMoments') %semicolon should not be there
WAPhaseMoments.KneeMoment = markers.LKneeMoments(WAStartFrame:WAEndFrame,:);
fprintf('\t\t\t Imported WA Knee Moments\n')
elseif exist('moments', 'var') && isfield(moments, 'LKneeMoments')
WAPhaseMoments.KneeMoment = moments.LKneeMoments(WAStartFrame:WAEndFrame,:);
fprintf('\t\t\t Imported WA Knee Moments\n')
end
Note that because of the shortcircuit behaviour of &&, isfield won't try to check the field of an inexistant variable if exist is false.
2 Kommentare
Guillaume
am 4 Mai 2017
"if I use elseif instead of else it does not run any code but just skips down to the next line"
which means that LKneeMoments is not a field of either variable. If you want a warning when that happens:
if isfield ...
...
elseif isfield ....
...
else
warning('LKneeMoments not found in either moments or markers');
end
Using else instead of elseif makes absolutely no sense. The else branch would be equivalent to
else
isfield(moments, 'LKneeMoments');
WAPhaseMoments.KneeMoment = moments.LKneeMoments(WAStartFrame:WAEndFrame,:);
which basically asks whether LKneeMoments is a field of moments and immediately discards the answer. Then gets the value of the field regardless of the answer. Of course, if the answer was that the field does not exist, then asking for its value will throw an error. Exactly what you saw.
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!