Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
i have a big matlab project, and i'm having trouble. here's my problem:

i have numerical text data (like a table in word or pages). how can i get this data in matlab? and how can i divide and add this data? (like you do with the formula editor in pages or excel)

i know you make cell arrays, but how do you add the contents of one cell with another?

thanks
 

deepy

macrumors regular
Jun 28, 2006
160
0
Dont know if this is what you're looking for but I have numerical data within a text file and to get it into a matrix i just use

load dataFile.txt
MatrixOfData = dataFile
 

Am3822

macrumors 6502
Aug 16, 2006
424
0
Groningen, The Netherlands
Matlab's cells are different from Excel's cells. If what you have is just a lot of numerical data in table form, deepy's suggestion should do the trick.

Once the matrix is in your workspace, you can manipulate individual entries, rows, or columns. You can find some more information in the online help.
 

deepy

macrumors regular
Jun 28, 2006
160
0
yup...once you've got it into a matrix you'll have to do some additionally post processing depending on what exactly you want to do. this sort of thing is usually quite quick and painless in matlab though.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
yup...once you've got it into a matrix you'll have to do some additionally post processing depending on what exactly you want to do. this sort of thing is usually quite quick and painless in matlab though.

well if i do get it in a matrix, how can i take the data from one cell, and add to the data in another cell, and then store the answer in a different cell?

basically just like you do in excel.
 

mduser63

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
well if i do get it in a matrix, how can i take the data from one cell, and add to the data in another cell, and then store the answer in a different cell?

basically just like you do in excel.

It's dead easy. A[3] = A[2] + A[1] will store the result of adding cells one and two of A in cell 3 of A. For multidimensional arrays, you just separate the indices to the arrays with a comma:

A[2, 1] = A[1, 1] + A[1, 2]
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
ok, well i'm having trouble getting my data in matlab. i'm getting this error:

??? Error using ==> vertcat
CAT arguments dimensions are not consistent.

i have 'strings' in my matrix. can i have this?
 

Am3822

macrumors 6502
Aug 16, 2006
424
0
Groningen, The Netherlands
Could you give some more information about the command that returned that error? Basically it means that you have tried to piece together two matrices/vectors with different lengths. Does this have anything to do with the strings you've mentioned? Matlab stores strings as vectors (one dimensional arrays) -- each entry holds a single character. Because of that, you cannot stack two strings one below the other (to form a two dimensional array) unless they have the same length. If this is not the case, the shorter one has to be padded with spaces.

On a more general note -- As mduser63 had said, Matlab is matrix/vector oriented (i.e. operation on whole matrices run faster than doing the same operation entry-by-entry, if such a thing is possible). This is very different from Excel's way of doing things.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
Could you give some more information about the command that returned that error? Basically it means that you have tried to piece together two matrices/vectors with different lengths. Does this have anything to do with the strings you've mentioned? Matlab stores strings as vectors (one dimensional arrays) -- each entry holds a single character. Because of that, you cannot stack two strings one below the other (to form a two dimensional array) unless they have the same length. If this is not the case, the shorter one has to be padded with spaces.

On a more general note -- As mduser63 had said, Matlab is matrix/vector oriented (i.e. operation on whole matrices run faster than doing the same operation entry-by-entry, if such a thing is possible). This is very different from Excel's way of doing things.

thanks for the response. i just took the strings out, so i don't get the error anymore. i'm still having problems though.

after i run the .m file, and then type the name of the matrix, it doesn't just display the numbers. it says:

1.0e+03 * and then displays numbers, but not the same numbers.

what am i doing wrong? with a smaller matrix, i don't get this
 

Am3822

macrumors 6502
Aug 16, 2006
424
0
Groningen, The Netherlands
The 1.0e+03* in the front is a result of matlab trying to fit as many columns of your matrix as possible into the screen. You can have some degree of control on how matlab displays onscreen numerical data using the format command.

One more thing, you cannot mix numerical values and strings in the same matrix (for that you need to use a struct or a cell-array, which are a bit like a C++ object).
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
The 1.0e+03* in the front is a result of matlab trying to fit as many columns of your matrix as possible into the screen. You can have some degree of control on how matlab displays onscreen numerical data using the format command.

One more thing, you cannot mix numerical values and strings in the same matrix (for that you need to use a struct or a cell-array, which are a bit like a C++ object).

thanks, after trying "format long" and then typing the matrix, i still get
1.0e+03*, i just get a lot of zeros on the end of the numbers, but the numbers are still off
 

Am3822

macrumors 6502
Aug 16, 2006
424
0
Groningen, The Netherlands
It appears that your numerical data has at least one large entry, which causes the 1.0e+03 rescaling to appear. The rest of small entries cannot be displayed properly then. Is this the way the data should be?
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
It appears that your numerical data has at least one large entry, which causes the 1.0e+03 rescaling to appear. The rest of small entries cannot be displayed properly then. Is this the way the data should be?

well, my smallest number is around 5.31, and the biggest number is 9469. is there any way to display them correctly?
 

Am3822

macrumors 6502
Aug 16, 2006
424
0
Groningen, The Netherlands
I don't really know. You might want to try the following: (a) use 'format long g' instead of 'format long'. (b) increase the window size for matlab's desktop and (c) decrease the font.

If all this fails, you could try to write a simple matlab script that would use the command sprintf to print the matrix, row by row, to the screen.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
I don't really know. You might want to try the following: (a) use 'format long g' instead of 'format long'. (b) increase the window size for matlab's desktop and (c) decrease the font.

If all this fails, you could try to write a simple matlab script that would use the command sprintf to print the matrix, row by row, to the screen.

thanks, 'format long g' worked, accept it only displayed 4 columns on the first line, then the next 4 on the next line. but at least it worked.

hopefully i can figure out how to create another matrix, based on this one, with adding and dividing to make the contents of the new matrix
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
well thanks everyone for the help. i finally figured out how to do it. now i've got all the data in there, and it works great. now i just have to finish making the program. i might have more questions, but til then, thanks ;) :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.