在 Matlab 中将元胞数组打印为 .txt
我有一个元胞数组,需要根据特定格式打印在 .txt 文件中。我已经尝试了一些在线帮助(包括 matlab 中央 dlmcell ,但即使这样也没有给我想要的答案。分隔符是 \t。
cellarray = { ...
'AAPL' '2/20/2011' 100.5
'MSFT' '2/15/2011' 43.4551
} ;
输出应位于具有以下格式的 .txt 文件中: (使用制表符分隔符)
"AAPL" "2/20/2011" 100.5
"MSFT" "2/15/2011" 43.4551
单元格的行数最少为 8000 行,最多为 15000 行。是否可以使用矢量化解决方案?
I have a cell array that needs to be printed in a .txt file according to a specific format. I have tried some of the online help (including matlab central dlmcell
but even that is not giving me the desired answer. Delimiter is \t.
cellarray = { ...
'AAPL' '2/20/2011' 100.5
'MSFT' '2/15/2011' 43.4551
} ;
The output should be in a .txt file with the following format: (using tab delimiter)
"AAPL" "2/20/2011" 100.5
"MSFT" "2/15/2011" 43.4551
The cell will have minimum of 8000 rows and a maximum of 15000. No rows would have empty columns. Is a vectorized solution possible? Shall appreciate your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容适用于您的示例:
MATLAB 重复使用格式化字符串,直到用完输入。原则上,您可以首先构造格式化字符串:
然后
The following will work for your example:
MATLAB reuses the formatting string until it runs out of inputs. In principle, you could construct the formatting string first:
Then
我经常结合使用 DLMWRITE 和 XLSWRITE。 DLMWRITE 用于制作文本文件,因此 XLSWRITE 不会生成 Excel 格式的文件,而是将其保留为文本。
请注意,在 COM 不可用的操作系统(如 Linux)上,XLSWRITE 写入文本,因此您不需要调用 DLMWRITE。
更新
此方法不再适用于最新版本的 MATLAB(可能从 R2012b 开始)。
xlswrite
始终写入 Excel 格式的文件。但是,自 R2013b 起,MATLAB 引入了 TABLES 和 WRITETABLE 如果元胞数组可以转换为表格(检查 CELL2TABLE 函数)。
I often use combination of DLMWRITE and XLSWRITE. DLMWRITE is used to make a text file, so XLSWRITE will not generate the file in Excel format, but will leave it as a text.
Note that on OS where COM is not available (like Linux) XLSWRITE write to text, so you don't need the call to DLMWRITE.
UPDATE
This method does not work anymore with the latest versions of MATLAB (since probably R2012b).
xlswrite
always writes to Excel-formatted file.However, since R2013b MATLAB introduced TABLES, and WRITETABLE function works great if cell array can be converted to a table (check CELL2TABLE function).