Finding index if exist
Show older comments
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)
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
on 7 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
on 21 Nov 2018
Jan
on 21 Nov 2018
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.
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!