在Matlab中将表格对象写入CSV

时间:2018-08-23 12:25:59

标签: matlab matlab-table

我在Matlab中有一个表格对象,其中包含单元格,如快照所示:

first entry

Land和Seamark内的单元格如下: enter image description here

enter image description here

对象的类别如下:

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

我尝试了类似writetablecsvwrite的语法,但是我没有得到正确的输出格式。如图所示,对Land和Seamark的读取变得混乱(读取是按列而不是按行)。

我希望我的输出按此顺序排列:

[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]

到目前为止的代码:

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

1 个答案:

答案 0 :(得分:1)

您可以简单地在二维矩阵的转置上使用reshape来构建新表:

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)

mycsv.csv文件的第一行也是唯一的行是

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

我使用WriteVariableNames,false Name-Value pair表示变量名不包含在文件的第一行中。