Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
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 Comments
Adam
Adam on 30 Apr 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

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!