Update scatter plot with different colors set by the user

조회 수: 3 (최근 30일)
susana
susana 2017년 9월 15일
댓글: susana 2017년 9월 20일
Hello,
I have a Gui that is plotting a scatter data into an axes. I want to be able to update the scatter with different colors by a depth condition (minfm, maxfm). The code is as follows:
scatter=findobj(eixo.Children,'type','Scatter');
minfm=[1000;2500];
maxfm=[2000;4000];
color={'blue';'green'};
[l,~]=size(scatter.YData);
[lin,~]=size(color);
V=repmat({'black'},1,l);
for i=1:lin
for j=1:l
if Y(j)>minfm(i) && Y(j)<maxfm(i)
V{:,j}=color{i,1};
end
end
end
set(scatter,'MarkerFaceColor',V{:});
I get the following error:
Error using matlab.graphics.chart.primitive.Scatter/set
There is no green property on the Scatter class.
The vector is ok, but I always get this error and it only plots everything in green

채택된 답변

Walter Roberson
Walter Roberson 2017년 9월 15일
편집: Walter Roberson 2017년 9월 15일
scatter=findobj(eixo.Children,'type','Scatter');
minfm=[1000;2500];
maxfm=[2000;4000];
color = [0 0 1; 0 1 0]; %{'blue';'green'};
[l,~]=size(scatter.YData);
[lin,~]=size(color);
V=repmat([0 0 0],l,1); %{'black'}
for i=1:lin
for j=1:l
if Y(j)>minfm(i) && Y(j)<maxfm(i)
V(j,:)=color(i,:);
end
end
end
set(scatter,'MarkerFaceColor',V);
It looks to me as if you should be able to condense that quite a lot.
color = [0 0 0; 0 0 1; 0 1 0];
Vidx = 1 + (Y > minfm(1) & Y < maxfm(1)) * 1 + (Y > minfm(2) & Y < maxfm(2)) * 2; %assume the ranges are mutually exclusive
V = color(Vidx(:), :);
  댓글 수: 2
susana
susana 2017년 9월 15일
Hello,
I had try that solution already. I got the following error:
Error using matlab.graphics.chart.primitive.Scatter/set Error setting property 'MarkerFaceColor' of class 'Scatter': Color value must be a 3 element numeric vector
I've checked and the vector is ok (nx3 double)... I cannot condensate the code like that because I don't have fixed depth intervals. The user may add as many intervals as he wants.
susana
susana 2017년 9월 20일
I've solve the problem. If you want to specify multiple colors of your scatter plot it is not possible to just set 'MarkerFaceColor' to multiple different colors. For a single scatter plot with different colors for the markers the 'CData' property to a Nx3 matrix of RGB values (not color strings) needs to be set. Plus, the previous MarkerFaceColor and MarkerEdgeColor needs to be set 'flat' prior to set the scatter with the new color property.. So it will be something like this: set(scatter,'MarkerFaceColor','flat') set(scatter,'MarkerEdgeColor','flat') set(scatter,'CData',new_vector)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by