1

I have an input data in Excel which has 2000 rows and 60 columns.

I want to read this data into MATLAB but I need to to interchange the rows and the column so that the matrix will be 60 rows and 2000 columns. How can I do this in MATLAB, because Excel only has 256 column which cannot hold 2000 columns.

Mola
  • 19
  • 2
  • 6
  • 1
    Excel 2007 can read over 2000 columns – Faken May 04 '10 at 19:26
  • Ouch, my eyes! Please format your code. – Doresoom May 05 '10 at 18:43
  • How can i? i AM NEW TO THIS FORUM, so teall me how i can insert a code here clearly – Mola May 05 '10 at 19:01
  • Thanks Experts, i think i have solve it. I just copy and past and transpose in excel and i have my columns in rows and my rows in column. I converted that results to txt and i have my program working. Thanks for you help – Mola May 05 '10 at 19:32

2 Answers2

4

You just need to transpose it: data = data'

tzaman
  • 46,925
  • 11
  • 90
  • 115
  • 3
    Note that you should use the `.'` operator for a simple array transpose, since the `'` operator will perform a complex conjugate transpose if the data values are complex. – gnovice May 04 '10 at 20:00
  • i did that but i have errors. Let me explain what i amtrying to do. I have a data which is 2000 rows and 60 columns. I am trying to plot labelMap for clustering but my program looks at a column and find i or zero to be able to classify data into this two clusters. But the 1 or zeros are inserted as extra row which makes it 2001 rows and 60 columns. i want to transpose this 2001 as rows so that the program will look at the 2001th column and the perform its classification and plot the label map.But anytime i run it, i have errors – Mola May 04 '10 at 20:05
  • Here are the errors: Error in ==> showMap at 36 figure,plot(cluster1(:,1),n+1-cluster1(:,2),'^b',cluster2(:,1),n+1-cluster2(:,2),'og'); Error in ==> mainfunction at 108 showMap(Map,LabelMap,map_size,data,col); I don't know what? and the figure does not plot. – Mola May 04 '10 at 20:07
  • My data is here: http://rapidshare.com/files/383549074/data.xls.html you can change it from .xls to .txt to be able to read it with my program. Here are the two programs involved: Mainfunction: http://rapidshare.com/files/383553075/mainfunction.txt.html the showMap function: http://rapidshare.com/files/383553389/showMap.txt.html Please kinly run them and help me see why i keep having the errors above Thanks – Mola May 04 '10 at 20:07
  • Can someone just look at my programs for me and see if he/she can see the problem of my error which i am not seeing. I really don't know. Thanks – Mola May 04 '10 at 20:43
  • 1
    @Mola: Please edit your question and paste the code directly there. – tzaman May 04 '10 at 21:17
0

To read in the data to MATLAB, start with the xlsread function. Then transpose it, as tzaman showed in his solution.

Your code might look like this:

[filename,path]=uigetfile();
data=xlsread([path,filename]);
data=data';
xlswrite([path,'myfile.xls'],data);

Which would save the transposed data as myfile.xls in the same directory as the original file.

EDIT: Excel 2003 is limited to 256 columns, which is why xlswrite is throwing an error. Have you tried using dlmwrite instead?

Community
  • 1
  • 1
Doresoom
  • 7,398
  • 14
  • 47
  • 61
  • BTW, the code Doresoom suggest gives an error which goes thus: ??? Error using ==> xlswrite Error: Object returned error code: 0x800A03EC i guess excel cannot handle more than 256 column and the number of columns in myfile.xls is supposed to be 2001. How can i solve this problem? – Mola May 04 '10 at 20:42
  • @Mola: I just double checked and ran my code w/ 2001 columns, no problem. What version of Excel are you using? Also, if xlswrite keeps throwing an error, and you just want access to the data, you can manipulate it in MATLAB after loading it with the first 3 lines of my example. – Doresoom May 04 '10 at 21:54
  • I want to have it write out. I don't know what i am doing wrong but it gives me error. Am using excel 2003. I still have the same error. When i run the code, it asked me to go to the path where i have the code and i click on the m file and it give the error. ??? Error using ==> xlswrite Error: Object returned error code: 0x800A03EC Error in ==> testing at 4 xlswrite([path,'myfile.xls'],data); I created your code as a separate M-file to read and write out my data. Any idea – Mola May 05 '10 at 17:57