Plot out of cell array with attributes

Hi, at first here is my code:
x = zeros(1979,1);
y = zeros(1979,1);
B = cell(1979,1);
for i = 1 : 1979
x(i) = i;
y(i) = i;
if i > 150
B(i,1) = {'bs'};
else
B(i,1) = {'ks'};
end
end
plot(x,y,B(:));
so the x vector contains my x-coordinates and the y-vector my y-coordinates. Now for example I want to plot the first 150 as a blue square and the others should be a black square.
I now that I can do it with a for-loop but in my real case the attributes depend on a a lot of things. So I want to create a cell array which contains the color and the appearance of the marker for each coordinates and to plot it automatically without a for-loop.
I hope you understand what I want to do :)

7 Comments

Hi there,
I'm not quite clear to me what you are after. It looks a bit like you are about to plot a line with a slope of 1 from the origin with the first bit being blue and the remainder being black.
Is this what you are trying to do?
I think dynamically assigning color to a plot() is a little difficult. I would look at imshow or scatter. There you can assign CData and change the colors of every point you have plotted.
Good luck!
Yes that's how the example should look like. But the general idea is that i want to plot it with only one command that assigns every point of the plot its specific color.
I looked at the scatter function but I'm still not sure how to change the color there. Could you explain it?
EDIT: ok I think i got it. Thanks for your help :)
x = zeros(1979,1);
y = zeros(1979,1);
B = cell(1979,1);
for i = 1 : 1979
x(i) = i;
y(i) = i;
if i > 150
B(i,1) = {[0 0 1]};
else
B(i,1) = {[0 0 0]};
end
end
C = cell2mat(B);
scatter(x,y,'CDataSource','C');
refreshdata;
So that works for the color. Do you have any idea how i can do the same thing with the markerstyle? For example i want the first 500 squares and the last ones circles?
Call scatter() twice - once for the first 500 with square marker shape, then again for the remaining points with a circle marker.
Nick Counts
Nick Counts on 20 Nov 2016
Edited: Nick Counts on 21 Nov 2016
@ Image
I agree yours is the most straightforward method. However, he asked for a one-command plot. It would be helpful to understand why that is a requirement
Maier
Maier on 23 Nov 2016
Edited: Maier on 23 Nov 2016
It is a requirement because I've got a lot of data to plot and if I plot it point by point it is very slow and the plot gets really laggy. Also every point is assigned to a context menu which is individual for every point.
I didn't say call scatter hundreds of times to plot hundreds of points point-by-point. I'm saying call it twice. However if you need a custom, unique, totally different pop-up context menu for every single one of the points, instead of one context menu that applies to any/all points, then I don't know - I've never done that before and doubt I'll ever need to do it in the future either. I can't envision any scenario where you'd need a different "context menu which is individual for every point."
Ok anyway thank you.
I'm writing a script to visualize the power flow solutions of an european electrical transmission grid. Every point is a substation of the grid and the context menu contains Information about it (e.g. voltage, name,...) which is individual for every point. Also the markerstyle and color depends from point to point. I'm doing it with a context menu because it gets a lot messy if I would do it any other way.

Sign in to comment.

Answers (1)

Nick Counts
Nick Counts on 19 Nov 2016
Edited: Nick Counts on 19 Nov 2016
I think you are looking for gscatter, which allows you to define a "group vector" and then define different styles for each group. It works sort of like logical indexing. There is good documentation, but here is an example:
x = 1:1979;
y = 1:1979;
% Make the group vector.
% The first 150 points are group 1
% The rest are group 2
group(1:150) = 1;
group(151:1979) = 2;
% The colors are assigned blue to 1, black to 2
% The marker styles are square to 1, circle to 2
gscatter(x, y, group, 'bk', 'so')
Hope this helps!

2 Comments

Hi, thank you! Yes that helps in case of the color and the marker style but I want to add more attributes than these two.
I want something like that:
plot(x,y,'Color',color,'marker',marker,'UiContextmenu',menu....)
where x and y contain the coordinates, color the color attribute and so on... I don't know if thats possible at all.
If you want to control a whole bunch more stuff on a data point-by-data point basis, you're going to have to plot it one data point at a time.

Sign in to comment.

Categories

Asked:

on 18 Nov 2016

Commented:

on 23 Nov 2016

Community Treasure Hunt

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

Start Hunting!