Thread Subject: add row to cell with different number of elements

Subject: add row to cell with different number of elements

From: Christian Kuijlaars

Date: 10 Feb, 2008 18:18:02

Message: 1 of 3

Hi,

This is the case

at the moment I have a structure in this form

str.A
str.B
str.C

A and B always contain only one string array. But C might
contain multiple. What I want is to produce a matrix that
looks like this

1 A B C
2 A B C C C
3 A B C C C C C C C
4 A B C C
5 A B C
6 A B C C C C
etc

Now the first part is easy, I make two vectors of A and B
and add those to the cell array. But to construct the
matrix for C is an entirely different matter. I now can do
it with a for loop but as those slow down the process I was
wondering wether this was simply possible with logical
indexing? I've tried

vec = str(1).C';
y(1) = vec;

however that gives the error that the number of elements
must be the same.
I can do y{1} = vec; but that gives me a cell in a cell and
the purpose of y is to import it in Excel through excel
link.

Hopefully it can be done because at the moment I'm working
with huge loads of data and that for loop really kills the
speed.

Subject: add row to cell with different number of elements

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 10 Feb, 2008 18:46:18

Message: 2 of 3

In article <fonf4q$t6n$1@fred.mathworks.com>,
Christian Kuijlaars <c.a.j.kuijlaars@student.tudelft.nl> wrote:

>at the moment I have a structure in this form

>str.A
>str.B
>str.C

>A and B always contain only one string array. But C might
>contain multiple. What I want is to produce a matrix that
>looks like this

>1 A B C
>2 A B C C C
>3 A B C C C C C C C
>4 A B C C
>5 A B C
>6 A B C C C C
>etc

Are we to understand that you want to produce a matrix in
which the K'th element is str(K).A str(K).B str(K).C

>Now the first part is easy, I make two vectors of A and B
>and add those to the cell array. But to construct the
>matrix for C is an entirely different matter. I now can do
>it with a for loop but as those slow down the process I was
>wondering wether this was simply possible with logical
>indexing? I've tried
>
>vec = str(1).C';
>y(1) = vec;
>
>however that gives the error that the number of elements
>must be the same.
>I can do y{1} = vec; but that gives me a cell in a cell and

Sorry, you need to be clearer on exactly what type str.A
and str.B and str.C are, and exactly what type of output
you are attempting to create.

You say that A and B always contain only one string array.
Are str.A and str.B cell string arrays, or are they
character arrays of a single row each, or are they character
arrays with multiple rows and columns per entry? If
str(K).A has a different number of rows than str(K).B then
how do you want the output to be created?

>the purpose of y is to import it in Excel through excel
>link.

Is each entry in y(K) intended to be the string formed
by concatenating str(K).A then str(K).B then all of the
rows of str(K).C in order; with no spaces or delimeters
between the elements?

I have never used the excel COM object link. I do know, though,
that when you use csvwrite or dlmwrite that unless you are careful
and manage the excel cell delimeters yourself, Matlab will write
each character into its own cell. I do not know if that can be
gotten around with the excel COM link.

Your reference to "a cell in a cell" implies that you are forming
y as a cell array instead of as a character array. Does the excel
COM object allow you to send through cell arrays of any form?
Or will you need to conver it all to a character array before
sending it to excell?
--
  "The slogans of an inadequate criticism peddle ideas to fashion"
                                              -- Walter Benjamin

Subject: add row to cell with different number of elements

From: Christian Kuijlaars

Date: 10 Feb, 2008 19:38:02

Message: 3 of 3

To first adres your last question, the excel link does not
ask for that but only wants one string array (e.g. 'hello
world') in one cell. If a cell has multiple string arrays
(e.g. 'hello' 'world') then excel link can not import.

A and B are single cell string arrays ('hello world')
C is also a string array but sometimes it contains multiple
string arrays in one cell.

the structure looks like this

str(K): A - 'Hello'
        B - 'World'
        C - <1x5 cell>

for example.

And know I want to generate a list in which
A B C
'Hello' 'World' C1 C2 C3 C4 C5.

C is a string array. If done the excel link will take every
cell content (because it actually does not contain cells
arrays but string arrays) and put it in a single cell in
excel.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
logical indexing Christian Kuijlaars 10 Feb, 2008 13:20:13
cell array Christian Kuijlaars 10 Feb, 2008 13:20:13
structure array Christian Kuijlaars 10 Feb, 2008 13:20:13
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com