필터 지우기
필터 지우기

Logical Indexing of Cell Array containing matrices

조회 수: 1 (최근 30일)
Georg Söllinger
Georg Söllinger 2016년 11월 7일
댓글: Walter Roberson 2016년 11월 8일
Hi everyone,
I need help with indexing cell arrays. I have a cell array, which contains matrices in the first, a string in the second and three identifiers in the third-fifth columns (see attached screenshot)
. What I want to do is a plot of specific values, which are located in the matrix of the first column and want to access those values with logical indexing of the identifiers. As a matter of fact, my logical indexing fails, it is not as straight forward as for standard arrays.
See my code below:
plot(R1{(R1{:,3} == phi && R1{:,4} == layer && R1{:,5} == speed),1}(:,6), ...
R1{(R1{:,3} == phi && R1{:,4} == layer && R1{:,5} == speed),1}(:,3),'Color',color(1,:),'LineStyle','-','LineWidth',1)
I have already tried the solutions with strcmp, but this doesn't work either...
Thanks for your help in advance! Georg
  댓글 수: 3
Georg Söllinger
Georg Söllinger 2016년 11월 7일
Oh sorry, the error message shown in the picture was actually not that, what I wanted to show. Nevertheless, I have no clue, how matlab comes to the error 'abs', if I use the strcmp...
The error, which is thrown with my solution is (when executing R1{:,3} == phi): Error using == Too many input arguments.
As I execute the whole statement, the error message is equal.
KSSV
KSSV 2016년 11월 7일
Can you attach R1 data and say what is phi and layer?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 11월 7일
mask = cell2mat(R1(:,3)) == phi & cell2mat(R1(:,4) == layer & cell2mat(R1(:,5)) == speed);
subset = R1(mask,:);
plot(subset{1, 1}(:,6), subset{1, 1}(:,3), 'Color', color(1,:), 'LineStyle', '-', 'LineWidth', 1);
hold on
arrayfun(@(ROWIDX) plot(subset{ROWIDX, 1}(:,6), subset{ROWIDX, 1}(:,3), 'Color', color(1,:), 'LineStyle', '-', 'LineWidth', 1), 2:size(subset,1));
If you already have "hold on" in effect then you can use
mask = cell2mat(R1(:,3)) == phi & cell2mat(R1(:,4) == layer & cell2mat(R1(:,5)) == speed);
subset = R1(mask,:);
arrayfun(@(ROWIDX) plot(subset{ROWIDX, 1}(:,6), subset{ROWIDX, 1}(:,3), 'Color', color(1,:), 'LineStyle', '-', 'LineWidth', 1), 1:size(subset,1));
  댓글 수: 2
Georg Söllinger
Georg Söllinger 2016년 11월 8일
Thank you, your solution works perfectly, great!! But what does ROWIDX and arrayfun do, wouldn't it also work if I'd simply plot subset{1,1}?
Thanks again!
Walter Roberson
Walter Roberson 2016년 11월 8일
arrayfun is there to plot all the subsets. Each subset member is one row in which the condition was true.
I do not assume that only one line matches the criteria.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2016년 11월 7일
abs is not defined for cell. Convert it to a array/ matrix and then use abs.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by