# need more matlab help!

Discussion in 'Mac Programming' started by twoodcc, Nov 14, 2006.

1. ### twoodcc macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#1
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

2. ### deepy macrumors regular

Joined:
Jun 28, 2006
#2
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

MatrixOfData = dataFile

3. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#3
thanks, i'll try it. but can you add or divide certain cells?

4. ### Am3822 macrumors 6502

Joined:
Aug 16, 2006
Location:
Groningen, The Netherlands
#4
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.

5. ### deepy macrumors regular

Joined:
Jun 28, 2006
#5
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.

6. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#6
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.

7. ### mduser63 macrumors 68040

Joined:
Nov 9, 2004
Location:
Salt Lake City, UT
#7
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]

8. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#8
ok, thanks. i'll try it

9. ### mduser63 macrumors 68040

Joined:
Nov 9, 2004
Location:
Salt Lake City, UT
#9
I thought I should point out that the "mat" in Matlab is short for "matrix". Matlab is made to do matrix math, and just about anything you can think to do to/with a matrix can be done in Matlab, and it's usually pretty easy.

10. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#10
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?

11. ### Am3822 macrumors 6502

Joined:
Aug 16, 2006
Location:
Groningen, The Netherlands
#11
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.

12. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#12
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

13. ### Am3822 macrumors 6502

Joined:
Aug 16, 2006
Location:
Groningen, The Netherlands
#13
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).

14. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#14
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

15. ### Am3822 macrumors 6502

Joined:
Aug 16, 2006
Location:
Groningen, The Netherlands
#15
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?

16. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#16
well, my smallest number is around 5.31, and the biggest number is 9469. is there any way to display them correctly?

17. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#17
but there are numbers like 401.370. i guess they take up a lot of space, but i still think there should be a way to display them

18. ### Am3822 macrumors 6502

Joined:
Aug 16, 2006
Location:
Groningen, The Netherlands
#18
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.

19. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#19
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

20. ### twoodcc thread starter macrumors P6

Joined:
Feb 3, 2005
Location:
Right side of wrong
#20
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