3

I have a cell array which each cell is a point on (x,y) coordination (i.e, the cells are of size [1x2]). Is it possible to change it to matrix that those coordination points to be reserved?

Because when I used cell2mat, the peculiar coordination was change to the size of [1x1] while I need the coordinates.

my cell array is like this: [0,2] [0,2] [1,3] [-13,10] [1,4] [-1,5]

How I can change it to a vector that these coordinates can be used later for plotting?

4
  • 2
    Please provide your expected result format. Commented Jan 23, 2014 at 15:13
  • Maybe its better to ask that how I can plot a cell array while each cell is a peculiar coordinate on (x,y)? Commented Jan 23, 2014 at 15:22
  • 1
    @Biju Exactly as Luis has shown, and then plot(result(:,1),result(:,2),'*') Commented Jan 23, 2014 at 15:24
  • Thanks @Dan, I was just updating my answer :-) Commented Jan 23, 2014 at 15:26

3 Answers 3

6
>> myCell = {[1 2],[3 4],[5 6]}; %// example cell. Can have any size/shape
>> result = cell2mat(myCell(:)) %// linearize and then convert to matrix
result =
     1     2
     3     4
     5     6

To plot:

plot(result(:,1),result(:,2),'o') %// or change line spec
Sign up to request clarification or add additional context in comments.

3 Comments

thanks Luis, but I want to plot these coordinates of [x y], while they are in cell format and don't need to get them seperated
In your question you say "change it to matrix"... Anyway, see updated answer
@LuisMendo what about myCell = {[1 2 3],[3 4 ],[5 6 5 6 7]} then how can i convert this cell array to vector?
4

Another way to accomplish this:

c = {[1 2], [3 4], [5 6]};
v = vertcat(c{:});    % same as: cat(1,c{:})

plot(v(:,1), v(:,2), 'o')

Cell arrays in MATLAB can be expanded into a comma-separated list, so the above call is equivalent to: vertcat(c{1}, c{2}, c{3})

3 Comments

what about c= {[1 2 3],[3 4 ],[5 6 5 6 7]} then how can i convert this cell array to vector?
in your case, you can only flatten the cell array into a one-dimensional vector (1x10 vector) as v = [c{:}] or v = cat(2, c{:}). You see when concatenating arrays, the dimensions must be consistent; you can't have a "jagged" matrix
otherwise you can pad each cell with NaNs so that all rows have the same length before concatenation. You can find plenty of similar questions here on Stack Overflow, for example this one: stackoverflow.com/q/3054437/97160
0
myCell = {[0,2] [0,2] [1,3] [-13,10] [1,4] [-1,5]};
hold on;
cellfun(@(c) plot(c(1),c(2),'o'),myCell);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.