Converting a matrix to formatted text

版本 1.0.0.0 (1.3 KB) 作者: Renee
Converts a numeric matrix to text in a cell array using matlab format string. Option to remove zeros
204.0 次下载
更新时间 2012/6/14

查看许可证

When exporting a matrix of numbers for display in another program (eg. using xlswrite()), it may be useful to apply a format, such as limiting the number of decimals or adding external commands.

matlab limitations:
- xlswrite() does not have a format option.
- num2str() converts a row into a single string, which exports as one character per cell.
- num2str() can't be applied to a cell array.

This simple function converts each element to a character cell in an array using arrayfun() and a nested function that applies the format string with num2str().

Option: add the optional argument 'sparse' to convert 0 values to blank cells using cellfun() and a nested function.

The nested functions use variables available to the parent function.

sample run:
A = rand(3,5); A(2,1:3) = 0; % table with a few 0 values
txtA = makeTextTable(A,'%.2f'); % convert to text with 2 decimals

txtA = makeTextTable(A,'%.2f','sparse'); % convert to text with 2 decimals (no zeros)

引用格式

Renee (2026). Converting a matrix to formatted text (https://ww2.mathworks.cn/matlabcentral/fileexchange/37169-converting-a-matrix-to-formatted-text), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2011b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0