pmdav
pmdav

Reputation: 301

Write a table object into csv in matlab

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' , ';');

Upvotes: 2

Views: 180

Answers (1)

Paolo
Paolo

Reputation: 26074

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.

Upvotes: 1

Related Questions