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

Thread Subject:
one question about xlswrite and matrix including both text and numbers

Subject: one question about xlswrite and matrix including both text and numbers

From: Yi

Date: 8 Apr, 2010 15:26:23

Message: 1 of 5

Hi my Matlab friends:

How to add a title to a matrix.

I have a matrix A = [1 2 3;4 5 6;7 9 10];
and I want each column have a title, which should be like this

A2 = [purchase, sale, number
         1 2 3
         4 5 6
         7 9 10];

I try to use A = {'purchase','sale','number';1 2 3;4 5 6; 7 8 10};
but each number has a [] as shown below, which I don't like.

A2 =

    'purchase' 'sale' 'number'
    [ 1] [ 2] [ 3]
    [ 4] [ 5] [ 6]
    [ 7] [ 9] [ 10]

Is it possible for me to get the matrix A2.

Eventually, I want to export them into an excel sheet, if I have to output both the text and the numerical numbers, how could I implement in one step?

Right now, I am using xlswrite to output the text in the first row first and then output numerical number in another command because I cannot get the matrix both include text and number.

Best Regards

Yi


Thanks

Subject: one question about xlswrite and matrix including both text and numbers

From: us

Date: 8 Apr, 2010 15:39:05

Message: 2 of 5

"Yi " <hvachy@163.com> wrote in message <hpksiv$sm6$1@fred.mathworks.com>...
> Hi my Matlab friends:
>
> How to add a title to a matrix.
>
> I have a matrix A = [1 2 3;4 5 6;7 9 10];
> and I want each column have a title, which should be like this
>
> A2 = [purchase, sale, number
> 1 2 3
> 4 5 6
> 7 9 10];
>
> I try to use A = {'purchase','sale','number';1 2 3;4 5 6; 7 8 10};
> but each number has a [] as shown below, which I don't like.
>
> A2 =
>
> 'purchase' 'sale' 'number'
> [ 1] [ 2] [ 3]
> [ 4] [ 5] [ 6]
> [ 7] [ 9] [ 10]
>
> Is it possible for me to get the matrix A2.
>
> Eventually, I want to export them into an excel sheet, if I have to output both the text and the numerical numbers, how could I implement in one step?
>
> Right now, I am using xlswrite to output the text in the first row first and then output numerical number in another command because I cannot get the matrix both include text and number.
>
> Best Regards
>
> Yi
>
>
> Thanks

fortunately, it's very simple...

% create your CELL A as you did above...
% then

     xlswrite('your.xls',A);

us

Subject: one question about xlswrite and matrix including both text and numbers

From: Yi

Date: 8 Apr, 2010 15:58:08

Message: 3 of 5

Thanks, but the problems is I cannot get a matrix including both text and numerical values.

I want to know how to crate a matrix like this

A = [test1, test2, test3;
        1 2 3;
        4 5 6;
        7 8 10]

Do you know how to do it?

 
> fortunately, it's very simple...
>
> % create your CELL A as you did above...
> % then
>
> xlswrite('your.xls',A);
>
> us

Subject: one question about xlswrite and matrix including both text and numbers

From: us

Date: 8 Apr, 2010 16:12:03

Message: 4 of 5

"Yi " <hvachy@163.com> wrote in message <hpkueg$422$1@fred.mathworks.com>...
> Thanks, but the problems is I cannot get a matrix including both text and numerical values.
>
> I want to know how to crate a matrix like this
>
> A = [test1, test2, test3;
> 1 2 3;
> 4 5 6;
> 7 8 10]
>
> Do you know how to do it?

this is NOT possible - and - NOT necessary...
simply use the CELL array A2, which you created in your OP(!)...
then

     xlswrite('your.xls',A2);

us

Subject: one question about xlswrite and matrix including both text and numbers

From: Steven Lord

Date: 8 Apr, 2010 16:26:47

Message: 5 of 5


"Yi " <hvachy@163.com> wrote in message
news:hpkueg$422$1@fred.mathworks.com...
> Thanks, but the problems is I cannot get a matrix including both text and
> numerical values.

That is correct. ALL the elements in an array in MATLAB must be of the same
type. No exceptions. [To forestall the inevitable comments about cell
arrays, the _elements_ of a cell array are all cells; the _contents_ of
those elements may differ, however. It's a small but important
distinction.]

> I want to know how to crate a matrix like this
>
> A = [test1, test2, test3;
> 1 2 3;
> 4 5 6;
> 7 8 10]
>
> Do you know how to do it?

If 'test1', 'test2', and 'test3' are supposed to be strings, this will
(correctly) error, as you can't concatenate the first row (a 1-by-15 char
array, NOT a 1-by-3 "string" array -- there's no such data type) with the
remaining three (which form a 3-by-3 double array.) They don't have the
same number of columns.

You will need to use cell arrays as us suggested. Alternately if you have
Statistics Toolbox, you could look at the DATASET object, which has (among
many other features) named rows and columns.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

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.

Contact us