Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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

1 vue (au cours des 30 derniers jours)
Inti Vanmechelen
Inti Vanmechelen le 30 Avr 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
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 commentaires
Adam
Adam le 30 Avr 2019
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 le 30 Avr 2019
Thanks Adam!
I'm a newbie, learning every day...

Réponses (0)

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by