Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Confusion with adding to cell array

Asked by Harold

Harold

on 20 May 2013
Accepted Answer by Walter Roberson

Walter Roberson

I have a cell array with the structure as such,

linesegs = [1x2 double] [1x2 double]

I do some calculations on with some other data that is sent to a function within the current program using a function handle. This results in the following structure within the function.

B = [0 0 1 1];

I want insert this array in the next row of linesegs. Basically B would be split into two cells, one containing [0 0] and the other [1 1].

Everything that I've tried seems to be failing.

0 Comments

Harold

Harold

Products

2 Answers

Answer by Walter Roberson

Walter Roberson

on 20 May 2013
Accepted answer
linesegs(2,:) = mat2cell(B, 1, [2 2]);

5 Comments

Walter Roberson

Walter Roberson

on 21 May 2013

You should consider using cellfun() instead of arrayfun()

Image Analyst

Image Analyst

on 21 May 2013

Why are you messing around with the complication of a cell array anyway? As I see it, a regular numerical matrix would handle these line segment coordinates just fine, and be a lot simpler.

Harold

Harold

on 21 May 2013

I will have to post up my program that I'm working on since I am having some more problems with it as well. I well edited this post with the link in a bit.

http://www.mathworks.com/matlabcentral/answers/76455-program-to-connect-points-and-merge-coplanar-lines

Walter Roberson

Walter Roberson

Answer by Amith Kamath

Amith Kamath

on 20 May 2013

This should work:

 a = {[0 0], [0 0]}
 b = [1 2 3 4]
 a{1} = b(1:2) %choose the first two elements from b and assign to first in a.
 a{2} = b(3:4) %choose third and fourth

1 Comment

Walter Roberson

Walter Roberson

on 20 May 2013

Note that is not "the next row".

Amith Kamath

Amith Kamath

Contact us