Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Continue in a for loop if a string in a string array isn't present

조회 수: 1 (최근 30일)
Inti Vanmechelen
Inti Vanmechelen 2019년 4월 30일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi,
I currently have this code:
def = {'Acc_X','Acc_Y','Acc_Z','Gyr_X','Gyr_Y','Gyr_Z'};
RF = {'RF1','RF2','RF3','RF4'};
for k = 1:NSensors
figure();
suptitle('REACH FORWARD');
for q = 1: length(def)
for o = 1: length(RF)
subplot(2,3,q);
plot(H9.MEAN_SUBJECT.(RF{o})(k).(def{q}));
hold on;
plot(H17.MEAN_SUBJECT.(RF{o})(k).(def{q}),':');
hold on;
xlim([0 100]);
xlabel('Time (%)');
ylabel(sprintf('%s',(def{q})));
end
end
end
However, for H9, RF3 does not exist, so I am trying (and obviously failing) to make the for loop skip this string if it is not present. Here is my attempt:
RF = {'RF1','RF2','RF3','RF4'};
for k = 1:NSensors
figure();
suptitle('REACH FORWARD');
for q = 1: length(def)
for o = 1: length(RF)
tf = ismember(RF,(RF{o}));
if tf(o) == 1
continue
end
subplot(2,3,q);
plot(H9.MEAN_SUBJECT.(RF{o})(k).(def{q}));
hold on;
plot(H17.MEAN_SUBJECT.(RF{o})(k).(def{q}),':');
hold on;
xlim([0 100]);
xlabel('Time (%)');
ylabel(sprintf('%s',(def{q})));
end
end
end
This gives me empty figures and thus no output.
I am aware that the "ismember" command might not the best in this situation, but I cannot seem to find a valid alternative as "isNaN" is not applicable and "exist" does not wrok for strings.
Any help would be greatly appreciated.
Thanks in advance!
  댓글 수: 2
Adam
Adam 2019년 4월 30일
tf = ismember(RF,(RF{o}));
will surely never return false. You are testing if an element of an array is in that same array, which it will always be.
Judging from your comments and what you attempt to do in the code I assume you want a test that includes something more like
if isfield( H9, RF{o} )
...
end
Inti Vanmechelen
Inti Vanmechelen 2019년 4월 30일
Thanks Adam!
I'm a newbie, learning every day...

답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by