0

I have something like this:

p = plot([0 1], [0 1], [1 2], [1 2]);

I want to take each pair and append another number.

x = get(p, 'XData');
y = get(p, 'YData');

x1 = mat2cell([x{1} double(2)]);
y1 = mat2cell([y{1} double(2)]);

x2 = mat2cell([x{2} double(3)]);
y2 = mat2cell([y{2} double(3)]);

set(p, 'XData', [x1; x2], 'YData', [y1; y2]); % this does not work

drawnow;

'get' is giving me some data in a format and I am 'set'-ing back in the same format the data with one more value for each pair.

The error I get is: Conversion to double from cell is not possible.

Dan
  • 1,555
  • 2
  • 14
  • 30
  • What are you trying to do to the plot? What error do you get? – Eitan T Aug 26 '13 at 15:31
  • Add more values to the graphs and refresh the plot. – Dan Aug 26 '13 at 15:32
  • Could you give a (working) example of how the result would be produced if you wanted to plot the data including additions manually? – Dennis Jaheruddin Aug 26 '13 at 16:03
  • Sure, initially I have 2 graphs: G1: x: 0, 1 | y: 0, 1 G2: x: 1, 2 | y: 1, 2 After addition: G1: x: 0, 1, 2 | y: 0, 1, 2 G2: x: 1, 2, 3 | y: 1, 2, 3 – Dan Aug 26 '13 at 16:12

2 Answers2

1

Try redrawing the plot:

xcoor = cellfun(@horzcat, get(p, 'XData'), {2; 3}, 'UniformOutput', false);
ycoor = cellfun(@horzcat, get(p, 'YData'), {2; 3}, 'UniformOutput', false);
c = [xcoor; ycoor];
plot(c{:})
Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • "Data must be a single matrix Y or a list of pairs X,Y" Also I want to add values to the graphs I don't want to flatten them into a single one. – Dan Aug 26 '13 at 15:51
  • "Vectors must be the same lengths." – Dan Aug 26 '13 at 16:05
  • Oops, forgot to remove the handle `p` from the plot command. Please try again. – Eitan T Aug 26 '13 at 16:10
  • It seems it works on the example above what you wrote but for "p = plot([0 1 1], [0 1 0], [5 6 6], [5 6 5]);" not that great. – Dan Aug 26 '13 at 16:39
  • Why not? It adds a value to each cell array as desired, no? – Eitan T Aug 26 '13 at 19:05
  • Yes the values are added but the plot is not what is expected, I appreciate your answer though. – Dan Aug 27 '13 at 10:08
  • @usre2704 No problem. Perhaps it'll be more helpful to visualize the expected result next time :) – Eitan T Aug 27 '13 at 10:59
1

There are a number of different ways to fetch the current plot points and add to them. The first two lines of Eitan's answer (using cellfun) are one way. Here's one using cell2mat and num2cell:

newX = [2 3];  % New x values to add
newY = [2 3];  % New y values to add
X = num2cell([cell2mat(get(p,'XData')) newX(:)], 2);
Y = num2cell([cell2mat(get(p,'YData')) newY(:)], 2);

The key issue to note when using the set function on multiple handles is stated in this excerpt from the documentation:

set(H,pn,MxN_pv) sets n property values on each of m graphics objects, where m = length(H) and n is equal to the number of property names contained in the cell array pn. This allows you to set a given group of properties to different values on each object.

As a result, your single call to set has to look like this:

set(p, {'XData'}, X, {'YData'}, Y);

Note that length(p) is equal to 2, the property strings are placed in cell arrays, and X and Y are each 2-by-1 cell arrays.

Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • So that's how you call `set` with cell array inputs... :-) – Eitan T Aug 26 '13 at 19:06
  • Thanks for the answer, I understand and I don't want to bash the language because I'm not very experienced but I feel the documentation is not that great and most of the times I feel that I'm scratching my right ear with my left hand. – Dan Aug 27 '13 at 10:12