choose B or C or D if A does not exist - how to write this?
    2 次查看(过去 30 天)
  
       显示 更早的评论
    
Hi all,
I have the following:
switch joint        
    case 'Knee'
        prox = 'HipJC';
        dist = 'LEPI';
        wand = 'THPA' ;       
    end
side = {'R','L'};
for i = 1:length(side)
    wandMk = data.([side{i},wand]).line;
end
 I got the following error: Unrecognized field name "LTHPA", which is because 'LTHPA' does not exist in my structure. 
I could use 'THPP' or 'THDA' or 'THDP' instead of 'THPA', because I know these exist. Hence, how could I rewrite the above for this to run? 
For example:
if 'THPA' does not exist then
wand = 'THDA' or 'THDA' or 'THDP'
Thanks!
采纳的回答
  Paul
      
      
 2024-7-28
        Create an example structure
data.THDA = 1;data.THPP = 2;data.THDP = 3;
fnames = fieldnames(data)
Field to test
wand = "LTHPA"
Reassign if invalid field
if ~any(strcmp(wand,fnames))
    wand = "THDA"
end
data.(wand)
1 个评论
  Paul
      
      
 2024-7-28
				Use isfield instead of strcmp/fieldnames Thanks @Stephen23!  How did I not know about that function (rhetorical question)?
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


