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:
help with string and numerical value in a matrix

Subject: help with string and numerical value in a matrix

From: Shatrughan

Date: 15 Sep, 2010 20:22:05

Message: 1 of 11

Hi,

I am trying to put some header on a numerical matrix. Here is what I am trying and would want as a result. Any help will be much appreciated.

x=rand(5,5);
y={'a','b','c','d','e'};

res=[x;y];
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.

I don't understand this error !!

I want my result as this,

res = [a b c d e; x(1,:); x(2,:); x(3,:); x(4,:); x(5,:)];


Thanks in advance.

Shatrughan

Subject: help with string and numerical value in a matrix

From: Sean

Date: 15 Sep, 2010 20:37:02

Message: 2 of 11

"Shatrughan " <shatrughan.lsu@gmail.com> wrote in message <i6r9td$77j$1@fred.mathworks.com>...
> Hi,
>
> I am trying to put some header on a numerical matrix. Here is what I am trying and would want as a result. Any help will be much appreciated.
>
> x=rand(5,5);
> y=['a','b','c','d','e'];
>
> res=[x;y];
> ??? Error using ==> vertcat
> CAT arguments dimensions are not consistent.
>
> I don't understand this error !!
>
> I want my result as this,
>
> res = [a b c d e; x(1,:); x(2,:); x(3,:); x(4,:); x(5,:)];
>
>
> Thanks in advance.
>
> Shatrughan

You'll have to use cells, such as:

x=rand(5,5);
y=['abcde']
C = {y;x}

Subject: help with string and numerical value in a matrix

From: Shatrughan

Date: 15 Sep, 2010 21:17:02

Message: 3 of 11

Hi Sean,

Thanks for prompt reply, but this does not solve my problem. It is still giving me a structural array having strings in first cell and x matrix in second cell which is not what I want. I simply want to put the y as header of x.

here again the result I want,

res=[ a b c d e
       1.2 1.3 1.3 1.5 1.4
       1.2 1.3 1.3 1.5 1.4
       1.2 1.3 1.3 1.5 1.4
       1.2 1.3 1.3 1.5 1.4
       1.2 1.3 1.3 1.5 1.4 ]

This should be a single matrix. ultimately I want to write it in a excel file. If, ther is alternative way of doing this, that would also be appreciated.

Thanks,
Shatrughan



"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6rape$56k$1@fred.mathworks.com>...
> "Shatrughan " <shatrughan.lsu@gmail.com> wrote in message <i6r9td$77j$1@fred.mathworks.com>...
> > Hi,
> >
> > I am trying to put some header on a numerical matrix. Here is what I am trying and would want as a result. Any help will be much appreciated.
> >
> > x=rand(5,5);
> > y=['a','b','c','d','e'];
> >
> > res=[x;y];
> > ??? Error using ==> vertcat
> > CAT arguments dimensions are not consistent.
> >
> > I don't understand this error !!
> >
> > I want my result as this,
> >
> > res = [a b c d e; x(1,:); x(2,:); x(3,:); x(4,:); x(5,:)];
> >
> >
> > Thanks in advance.
> >
> > Shatrughan
>
> You'll have to use cells, such as:
>
> x=rand(5,5);
> y=['abcde']
> C = {y;x}

Subject: help with string and numerical value in a matrix

From: someone

Date: 15 Sep, 2010 21:31:21

Message: 4 of 11

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6rape$56k$1@fred.mathworks.com>...
> "Shatrughan " <shatrughan.lsu@gmail.com> wrote in message <i6r9td$77j$1@fred.mathworks.com>...
> > Hi,
> >
> > I am trying to put some header on a numerical matrix. Here is what I am trying and would want as a result. Any help will be much appreciated.
> >
> > x=rand(5,5);
> > y=['a','b','c','d','e'];
> >
> > res=[x;y];
> > ??? Error using ==> vertcat
> > CAT arguments dimensions are not consistent.
> >
> > I don't understand this error !!
> >
> > I want my result as this,
> >
> > res = [a b c d e; x(1,:); x(2,:); x(3,:); x(4,:); x(5,:)];
> >
> >
> > Thanks in advance.
> >
> > Shatrughan
>
> You'll have to use cells, such as:
>
> x=rand(5,5);
> y=['abcde']
> C = {y;x}

doc xlswrite

% Look at Example 2
% It shows you how to do exactly what you want.

Subject: help with string and numerical value in a matrix

From: Matt J

Date: 15 Sep, 2010 21:46:04

Message: 5 of 11

"Shatrughan " <shatrughan.lsu@gmail.com> wrote in message <i6rd4e$9d4$1@fred.mathworks.com>...
> Hi Sean,
>
> Thanks for prompt reply, but this does not solve my problem. It is still giving me a structural array having strings in first cell and x matrix in second cell which is not what I want. I simply want to put the y as header of x.
>
> here again the result I want,
>
> res=[ a b c d e
> 1.2 1.3 1.3 1.5 1.4
> 1.2 1.3 1.3 1.5 1.4
> 1.2 1.3 1.3 1.5 1.4
> 1.2 1.3 1.3 1.5 1.4
> 1.2 1.3 1.3 1.5 1.4 ]
>
> This should be a single matrix. ultimately I want to write it in a excel file. If, ther is alternative way of doing this, that would also be appreciated.
===============

Then do, for example

>> res=[num2cell('abcde');num2cell(rand(5))]

res =

    'a' 'b' 'c' 'd' 'e'
    [0.3210] [0.1036] [0.6178] [0.0550] [0.4215]
    [0.2553] [0.7456] [0.0729] [0.6668] [0.1432]
    [0.2584] [0.5926] [0.1291] [0.0539] [0.6195]
    [0.5255] [0.2561] [0.8011] [0.7981] [0.8319]
    [0.7349] [0.0713] [0.4839] [0.6215] [0.1098]


Or instead of trying to combining the headers and numeric data into a single array, use two calls to xlswrite, one to write the headers, the next to write the numeric data,e.g.

 
myFile='tst.xls';

Headers={'Apples','Oranges','Bananas'};

NumericData=rand(5,3);

xlswrite(myFile,Headers,1,'A1');
xlswrite(myFile,NumericData,1,'A2');

Subject: help with string and numerical value in a matrix

From: David Young

Date: 15 Sep, 2010 21:49:04

Message: 6 of 11

First, rather oddly, your original message has y as a cell array of strings (with curly braces), but when quoted in Sean's reply the curly braces have changed to square brackets, making y simply a string. I'm afraid this makes Sean's reply hard to follow.

Going back to your original post, the key question is what you want the result res to be.

One possibility is an array of double (a standard Matlab matrix, containing numerical values). In this case, the first row can contain the numerical codes for the characters 'a', 'b' etc. They'll then print as numbers rather than characters, andwill be interpreted in the spreadsheet as numbers, so this may not be what you want. If you do want to do this, the code would go:

  x=rand(5,5);
  y={'a','b','c','d','e'}; % your original setup

  ychars = [y{:}]; % make an array of characters
  res = [double(ychars); x] % make a header line

Alternatively, you can keep the distinction between numbers and strings by putting everything into a cell array, like this:

   res = [y; num2cell(x)]

Whether either of these is useful depends on how you are writing the data for your spreadsheet.

Subject: help with string and numerical value in a matrix

From: Shatrughan

Date: 15 Sep, 2010 22:04:04

Message: 7 of 11

Thanks to all the replies,

I figured it out using 'xlswrite' and 'fid' commands which helpmed me in finally writing into an excel file.

It si writing into an excel file perfectly, but giving me an error in MATLAB window saying "xlswrite is locked by some other program..." something like that, though I chose the option fopen as

fid=fopen ('data.xls','wt')

then using fprintf..

I have no idea about this error in MATLAB but glad that it is writing into the file which is what I ultimately need.

Should leave these errors for experts to work on...

Thanks for the replies though.

Shatrughan


"David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <i6rf0g$ci9$1@fred.mathworks.com>...
> First, rather oddly, your original message has y as a cell array of strings (with curly braces), but when quoted in Sean's reply the curly braces have changed to square brackets, making y simply a string. I'm afraid this makes Sean's reply hard to follow.
>
> Going back to your original post, the key question is what you want the result res to be.
>
> One possibility is an array of double (a standard Matlab matrix, containing numerical values). In this case, the first row can contain the numerical codes for the characters 'a', 'b' etc. They'll then print as numbers rather than characters, andwill be interpreted in the spreadsheet as numbers, so this may not be what you want. If you do want to do this, the code would go:
>
> x=rand(5,5);
> y={'a','b','c','d','e'}; % your original setup
>
> ychars = [y{:}]; % make an array of characters
> res = [double(ychars); x] % make a header line
>
> Alternatively, you can keep the distinction between numbers and strings by putting everything into a cell array, like this:
>
> res = [y; num2cell(x)]
>
> Whether either of these is useful depends on how you are writing the data for your spreadsheet.

Subject: help with string and numerical value in a matrix

From: Matt J

Date: 15 Sep, 2010 22:23:04

Message: 8 of 11

"Shatrughan " <shatrughan.lsu@gmail.com> wrote in message <i6rfsk$9cr$1@fred.mathworks.com>...
> Thanks to all the replies,
>
> I figured it out using 'xlswrite' and 'fid' commands which helpmed me in finally writing into an excel file.
>
> It si writing into an excel file perfectly, but giving me an error in MATLAB window saying "xlswrite is locked by some other program..."
=============

If the file you're writing to is currently opened in Excel, then Excel is locking the file. Close Excel and repeat the xlswrite()


>I chose the option fopen as
>
> fid=fopen ('data.xls','wt')
>
> then using fprintf..
===============

I'm surprised this is possible, but it seems like a bad idea.

Subject: help with string and numerical value in a matrix

From: Steven_Lord

Date: 16 Sep, 2010 17:38:05

Message: 9 of 11



"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
news:i6rh08$kku$1@fred.mathworks.com...
> "Shatrughan " <shatrughan.lsu@gmail.com> wrote in message
> <i6rfsk$9cr$1@fred.mathworks.com>...
>> Thanks to all the replies,
>>
>> I figured it out using 'xlswrite' and 'fid' commands which helpmed me in
>> finally writing into an excel file.
>>
>> It si writing into an excel file perfectly, but giving me an error in
>> MATLAB window saying "xlswrite is locked by some other program..."
> =============
>
> If the file you're writing to is currently opened in Excel, then Excel is
> locking the file. Close Excel and repeat the xlswrite()
>
>
>>I chose the option fopen as fid=fopen ('data.xls','wt')
>>
>> then using fprintf..
> ===============
>
> I'm surprised this is possible, but it seems like a bad idea.

I agree that it's probably a bad idea (since writing text data to a
Microsoft Excel file will almost certainly overwrite the header information
and make it unreadable by Excel) but why shouldn't this be possible?
data.xls is a valid file name. FOPEN doesn't (and IMO shouldn't) check to
see if the extension of the file name you pass in happens to be registered
in the Windows registry as a recognized file type and refuse to open the
file if you try to open it the wrong way. FOPEN isn't that smart and (again
IMO) shouldn't be.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: help with string and numerical value in a matrix

From: Matt J

Date: 16 Sep, 2010 18:14:04

Message: 10 of 11

"Steven_Lord" <slord@mathworks.com> wrote in message <i6tklt$4vs$1@fred.mathworks.com>...

> I agree that it's probably a bad idea (since writing text data to a
> Microsoft Excel file will almost certainly overwrite the header information
> and make it unreadable by Excel) but why shouldn't this be possible?
===============

Because as you say, the result should be unreadable by Excel. Yet the OP seems to be reporting that Excel is reading it successfully!

Subject: help with string and numerical value in a matrix

From: Steven_Lord

Date: 17 Sep, 2010 14:17:39

Message: 11 of 11



"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
news:i6tmpc$lld$1@fred.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <i6tklt$4vs$1@fred.mathworks.com>...
>
>> I agree that it's probably a bad idea (since writing text data to a
>> Microsoft Excel file will almost certainly overwrite the header
>> information and make it unreadable by Excel) but why shouldn't this be
>> possible?
> ===============
>
> Because as you say, the result should be unreadable by Excel. Yet the OP
> seems to be reporting that Excel is reading it successfully!

Well, the LOAD function in MATLAB can handle not only MAT-files but also
text files if their format is "nice enough", so perhaps Microsoft Excel's
internal I/O routines have a similar amount of smarts.

http://www.mathworks.com/help/techdoc/ref/load.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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