Finding index if exist

Hello,
I am trying to check if specific data is inside my variable and if it does - extract it. If it does not exist, the script should just proceed.
if true
% code
end
for ww = 1:mfiles
idx_r02r(ww) = find(contains(Data_description{1,(ww)},'RO*_Right'));
if ~isempty(idx_r02r)
R02_r{ww} = Data_measured{1,ww}{1,idx_r02r};
R02_r = cell2mat(R02_r);
R02_r = reshape(R02_r, [t,mfiles])';
elseif isempty(idx_r02r)
end
end
the script gives me an error 'Unable to perform assignment because the left and right sides have a different number of elements.' already for the second row of my script. I expect for example a vector idx_r02r = [0 0 0 1] if the measurement R0* exists for the fourth test subject. I tried removing ww from the left side of
idx_r02r(ww) = find(contains(Data_description{1,(ww)},'RO*_Right'));
and it gives me an empty matrix but it overwrites the variable for each test subject.
Can you help me here?

Answers (1)

Jan
Jan on 7 Nov 2018
Edited: Jan on 8 Nov 2018
idx_r02r(ww) = find(contains(Data_description{1,(ww)},'R0*_Right'));
This can work only, if exactly 1 matching string is found. Otherwise you have a scalar on the left and either an empty array or a vector with more than 1 element. Maybe you want:
idx_r02r = contains(Data_description{1, ww}, 'R0*_Right');
idx_r02l = contains(Data_description{1, ww}, 'R0*_Left');
R02_r{ww} = [Data_measured{1,ww}{1,idx_r02r}];
R02_l{ww} = [Data_measured{1,ww}{1,idx_r02l}];
But this is guessing only, because I'm not sure what you want to achieve.

4 Comments

KDRA
KDRA on 7 Nov 2018
What I want to do is:
  1. check if R0* measurement exists in variable called Data_description for all subject
  2. if yes - check in which row if is placed and for which subject (for example: idx = [0 0 0 3] would mean that that for first three subjects the measurement R0* does not exist but for the fourth subject this measurement is placed in the third row
  3. extract the data from the same row from the variable called Data_measured
this works perfectly when the measurement exists and idx is not empty but as soon as it is empty for the first subject it would not move on.
Jan
Jan on 8 Nov 2018
Edited: Jan on 8 Nov 2018
@KDRA: Remember, that the readers do not know, what you are doing. I cannot know, what "if R0* measurement exists" exactly means. Instead of explaining the meaning, focus on what happens inside Matlab, e.g.: "check if an element of the cell string Data_description is 'R0*_Right'" or "starts with 'R0*'".
"idx = [0 0 0 3]" is not clear also. How is idx defined? This variable does not occur in the code you have posted.
"It would not move on" is not clear. Post the complete error message instead.
Maybe you want:
idx_r02r = contains(Data_description{1, ww}, 'R0*_Right');
if any(idx_r02r)
...
end
Then the code inside the if block is evaluated only, if a matching element is found.
By the way, I'm not sure if it is "R0*" (r zero star) or "RO*" (r upper oh star). This is prone to typos.
I've explained already, why
idx_r02r(ww) = find(contains(Data_description{1,(ww)},'RO*_Right'))
fails, when no matching element is found. Did you read and understood this detail?
KDRA
KDRA on 21 Nov 2018
Hey Jan,
sorry for not being very specific in terms of MatLab language.
Indeed, I am trying to find out if any element of the cell string Data_description is 'RO*_Right'. idx is actually my Idx_r02_r and it specifies the indexes of the Data_description where 'RO*_Right' is found. Next step is extracting the data from Data_measured which have the same index as Idx_r02_r.
Thanks for pointing out the zeros and Oh confusion. I corrected it in my script.
I tried your solution with 'any' command. My script runs now and looks like this now:
for ww = 1:mfiles
idx_r02r = contains(Data_description{1,ww},'R0*_Right');
idx_r02l = contains(Data_description{1,ww},'R0*_Left');
if any(idx_r02r)
R02_r{ww} = Data_measured{1,ww}{1,idx_r02r};
R02_r = cell2mat(R02_r);
R02_r = reshape(R02_r, [t,mfiles])';
if any(idx_r02l)
R02_l{ww} = Data_measured{1,ww}{1,idx_r02l};
R02_l = cell2mat(R02_l);
R02_l = reshape(R02_l, [t,mfiles])';
end
end
end
But this doesn't help much as now it gives me logical values, not indexes (idx_r02r = 1×20 logical array 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0).
What I need is an index of a non zero element so in this case it would be idx_r02r = 1×20 logical array 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 because the non-zero element is in 9th column.
I'm not sure, what the problem is. Maybe you want:
% Example - use the variable obtained in your code...
idx_r02r = logical([0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0])
idx_r02r = idx_r02r .* 1:numel(idx_r02r);
On the other hand: Why is the logical indexing not sufficient in your case? In the shown code, setting idx_r02r to the numerical indices, e.g. filled with zeros, should be either failing or it is a wast of time. Logical indexing is usually faster.

Sign in to comment.

Categories

Asked:

on 7 Nov 2018

Commented:

Jan
on 21 Nov 2018

Community Treasure Hunt

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

Start Hunting!