0

this is my question:

A={[1 2 3]; [1] ; [5 1]};

Now I want to convert this variable into a matrix, like this:

[B]=functionX(A)

B= [1  2   3;
    1  NaN NaN;
    5  1   NaN];

Once the first question is solved, I wanna plot this matrix, so I have to do this (I don't know if there is a more simple way to do it):

figure;hold on
for i=1:size(B)
    plot(B(i,:),'o')
end

And then I get this graphic:

enter image description here

So, I was wondering if maybe there is a way to make that matlab recognize just one data, I mean, only appears in the legend "data 1" instead "data 1, data 2 and data 3".

lisandrojim
  • 509
  • 5
  • 18
  • look at this for the first part http://stackoverflow.com/questions/6210495/how-to-combine-vectors-of-different-length-in-a-cell-array-into-matrix-in-matlab. – Nishant Jun 20 '14 at 15:58
  • for the second part use `plot(repmat((1:size(B,1))',size(B,2),1),reshape(B',[],1),'O')` intsead of the for loop – Nishant Jun 20 '14 at 16:09

1 Answers1

1

the first part can be done like this

maxLength = max( cellfun(@(x)(numel(x)),A));
B = cell2mat(cellfun(@(x)cat(2,x,NaN*ones(1,maxLength -length(x))),A,'UniformOutput',false));

for the second part use this

plot(repmat((1:size(B,1))',size(B,2),1),reshape(B',[],1),'O')
Nishant
  • 2,571
  • 1
  • 17
  • 29