在 Matlab 中将元胞数组打印为 .txt

发布于 2024-12-22 05:39:23 字数 409 浏览 2 评论 0原文

我有一个元胞数组,需要根据特定格式打印在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2024-12-29 05:39:23

以下内容适用于您的示例:

C = cellarray.';
fid = fopen('file.dlm', 'wt');
fprintf(fid, '"%s"\t"%s"\t%g\n', C{:});
fclose(fid);

MATLAB 重复使用格式化字符串,直到用完输入。原则上,您可以首先构造格式化字符串:

fstr = '';
for ic = 1:size(cellarray,2)
   switch class(cellarray{1,ic})
       case 'char'
           fstr = [fstr '"%s"'];
       otherwise
           % Assume numeric
           fstr = [fstr '%g'];
   end
   if ic < size(cellarray,2), fstr = [fstr '\t']; else fstr = [fstr '\n']; end
end

然后

C = cellarray.';
fid = fopen('file.dlm', 'wt');
fprintf(fid, fstr, C{:});
fclose(fid);

The following will work for your example:

C = cellarray.';
fid = fopen('file.dlm', 'wt');
fprintf(fid, '"%s"\t"%s"\t%g\n', C{:});
fclose(fid);

MATLAB reuses the formatting string until it runs out of inputs. In principle, you could construct the formatting string first:

fstr = '';
for ic = 1:size(cellarray,2)
   switch class(cellarray{1,ic})
       case 'char'
           fstr = [fstr '"%s"'];
       otherwise
           % Assume numeric
           fstr = [fstr '%g'];
   end
   if ic < size(cellarray,2), fstr = [fstr '\t']; else fstr = [fstr '\n']; end
end

Then

C = cellarray.';
fid = fopen('file.dlm', 'wt');
fprintf(fid, fstr, C{:});
fclose(fid);
深者入戏 2024-12-29 05:39:23

我经常结合使用 DLMWRITE 和 XLSWRITE。 DLMWRITE 用于制作文本文件,因此 XLSWRITE 不会生成 Excel 格式的文件,而是将其保留为文本。

filename = 'file.txt';
dlmwrite(filename, 1)
xlswrite(filename, cellarray)

请注意,在 COM 不可用的操作系统(如 Linux)上,XLSWRITE 写入文本,因此您不需要调用 DLMWRITE。


更新

此方法不再适用于最新版本的 MATLAB(可能从 R2012b 开始)。 xlswrite 始终写入 Excel 格式的文件。

但是,自 R2013b 起,MATLAB 引入了 TABLESWRITETABLE 如果元胞数组可以转换为表格(检查 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.

filename = 'file.txt';
dlmwrite(filename, 1)
xlswrite(filename, cellarray)

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文