【问题标题】:Write a table object into csv in matlab在matlab中将表格对象写入csv
【发布时间】:2019-01-29 19:57:45
【问题描述】:

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

Land and Seamark里面的单元格如下:

对象的类别如下:

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

我尝试了一些语法,例如 writetablecsvwrite,但我没有得到正确的输出格式。如图所示,陆地和海标的读数变得混乱(读数是按列而不是按行)。

我希望我的输出按以下顺序:

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

【问题讨论】:

  • 请添加您尝试过的示例代码
  • writetable(FileData.gTruth.LabelData,'labelled1.txt','Delimiter' , ';');

标签: matlab matlab-table


【解决方案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,falseName-Value pair 表示变量名不包含在文件的第一行中。

【讨论】:

  • 在某些行中,我在 ship 和 seamark 中没有元素,因此它们是空单元格。在这种情况下,我会收到一个错误: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); 如何处理?
猜你喜欢
  • 2019-02-02
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多