2

I have a table object in Matlab with cells as shown in the snapshot:

first entry

The cells inside Land and Seamark are as below: enter image description here

enter image description here

The class of the objects are as below:

>> class(FileData.gTruth.LabelData.Land)
ans =
    'cell'
>> class(FileData.gTruth.LabelData.Land{1,1})
ans =
    'double'
>> class(FileData.gTruth.LabelData)
ans =
    'table'

I tried some syntax like writetable and csvwrite but i am not getting the right format of output. The reading of the Land and Seamark as shown in the figure gets jumbled(reading is columnwise and not row-wise).

I want my output to be in this order:

[1063 126 115 86] [1 169 158 147;1 104 165 66;728 105 276 43;950 113 971 40;1 107 810 23;227 133 48 15;618 131 107 20] [562 220 33 51;1736 167 26 28;532 130 18 15;393 129 23 14]

Code so far:

writetable(FileData.gTruth.LabelData,'labelled1.txt','Delimiter' , ';');

Prostagma
  • 1,756
  • 9
  • 21
pmdav
  • 301
  • 4
  • 14

1 Answers1

1

You can simply use reshape on the transpose of the two-dimensional matrices to build a new table:

Ship = [1063 126 115 86]
Land = {[1 169 158 147;1 104 165 66; 728 105 276 43; 950 113 971 40; 1 107 810 23; 227 133 48 15; 618 131 107 20]}
Seamark = {[562 220 33 51; 1736 167 26 28; 532 130 18 15; 393 129 23 14]}

t = table(Ship,Land,Seamark);
t2 = table(t.Ship,reshape(t.Land{:}.',1,[]),reshape(t.Seamark{:}.',1,[]))

writetable(t2,'mycsv.csv','WriteVariableNames',false)

The first and only row of mycsv.csv file is:

1063 126 115 86 1 169 158 147 1 104 165 66 728 105 276 43 950 113 971 40 1 107 810 23 227 133 48 15 618 131 107 20 562 220 33 51 1736 167 26 28 532 130 18 15 393 129 23 14

I used the WriteVariableNames,false Name-Value pair to indicate that the variable names are not to be included in the first row of the file.

Paolo
  • 21,270
  • 6
  • 38
  • 69
  • In some of the rows, i have no elements in ship and in seamark so they are empty cells. In such case i get an error: `Error using tabular/verifyCountVars (line 339) All variables must have the same number of rows. Error in table (line 276) numRows = tabular.verifyCountVars(vars); Error in labelled (line 9) t=table(Ship,Land,Seamark);` How to handle that? – pmdav Aug 24 '18 at 09:45