How to convert a cell array containing character and double data into a matrix?

8 views (last 30 days)
I am working on an assignment for my intro matlab course at school and i've run into a problem. I have accomplished the goal of the assignment, which is to take in data that they give us about the medals in the 2010 vancouver olympics and display which country won how many of each medals, and the total medals of each country. But for some reason when i display the cell array that my program creates it shows up like this:
'AUS' [ 0] [ 1] [ 2] [ 3]
'AUT' [ 6] [ 6] [ 4] [16]
'BLR' [ 1] [ 1] [ 1] [ 3]
'CAN' [ 5] [ 7] [14] [26]
'CHN' [ 4] [ 2] [ 5] [11]
'CRO' [ 1] [ 2] [ 0] [ 3]
'CZE' [ 4] [ 0] [ 2] [ 6]
'EST' [ 0] [ 1] [ 0] [ 1]
'FIN' [ 4] [ 1] [ 0] [ 5]
'FRA' [ 6] [ 3] [ 2] [11]
'GBR' [ 0] [ 0] [ 1] [ 1]
'GER' [ 7] [13] [10] [30]
'ITA' [ 3] [ 1] [ 1] [ 5]
'JPN' [ 2] [ 3] [ 0] [ 5]
'KAZ' [ 0] [ 1] [ 0] [ 1]
'KOR' [ 2] [ 6] [ 6] [14]
'LAT' [ 0] [ 2] [ 0] [ 2]
'NED' [ 3] [ 1] [ 4] [ 8]
'NOR' [ 6] [ 8] [ 9] [23]
'POL' [ 2] [ 3] [ 1] [ 6]
'RUS' [ 7] [ 5] [ 3] [15]
'SLO' [ 1] [ 2] [ 0] [ 3]
'SUI' [ 3] [ 0] [ 6] [ 9]
'SVK' [ 1] [ 1] [ 1] [ 3]
'SWE' [ 4] [ 2] [ 5] [11]
'USA' [13] [15] [ 9] [37]
I would like to know why the ' ' characters show up on all the string entries, and why the [] characters show up on all the double entries. It seems to me to be a property of a cell array, because when I convert each individual column into a regular matrix and display it the unwanted characters vanish. However I can not figure out how to recombine them into an array after doing this with the same formatting as the above, just without the unwanted characters. The issue i run into in attempting this is that you can't combine unlike data types into a regular matrix, and i think that even if i could figure out a way it would mess with the formatting. It appears that i would have to separate the columns, somehow change either the character data into doubles(or the reverse) and then recombine them. Any information on how to solve this problem would be greatly appreciated.
Just in case it was unclear, I want it to look like:
AUS 0 1 2 3
...
Thanks

Answers (1)

Walter Roberson
Walter Roberson on 23 Mar 2014
You cannot mix numbers and strings in the same array. You can have cells that contain different types within the cell array, but when you disp() that, the strings will show up quoted and the arrays will show up with [] around them.
There are two possible cures:
  1. arrange to display each line in the format you want using fprintf(); or
  2. convert everything into a string array and store or display that array
See fprintf() and sprintf()

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!