File Exchange

image thumbnail

cprintf: a pedestrian ND array formatting and display utility

version (62.5 KB) by us
cprintf converts an ND array or ND cell array to a 2D character array and - optionally - a table

1 Download

Updated 11 Jun 2009

View Version History

View License

- converts
an ND array of
- real and/or complex full numeric data
- real and/or complex sparse numeric data
- logical data
- char strings
- structures
- other objects
or an ND cell array of any combination of the above
to a 2D character array
- creates tables
- the input may be a cell array formatted as a table with
row/column labels and row/column separators
- any simple (ND) array can be formatted as a table using
any combination of
row/column labels and row/column separators

many options allow to format each aspect of the input and output

to better organize a project using CPRINTF
- options may be entered by an option structure
- options may be read from any ASCII file
- options may be saved in an executable m-file, which
returns an option structure

saving output
- the output may be saved in an ASCII file
- optionally, a file template may be used to insert the
result at a marked position
- tokens in a file template may be replaced by user defined
strings during runtime

CPRINTF may be a useful front end engine to create input for a spreadsheet program

> help cprintf
and the accompanying published M-file for more information and examples

> cphelp
to open the published M-file in a ML browser window

> cpdoc
to run the examples used in the published M-file

Cite As

us (2020). cprintf: a pedestrian ND array formatting and display utility (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)

Felipe G. Nievinski

Would you consider replacing this block (~ l.1050):

v=par.s2f('@(x) x'); % WARNING only
v(10); % ERROR to catch
catch %#ok

for this one:

[msg_str_old, msg_id_old] = lastwarn('', '');
par.s2f('@(x) x'); % WARNING only
[msg_str, msg_id] = lastwarn(msg_str_old, msg_id_old);
if strcmp(msg_id, 'MATLAB:str2func:invalidFunctionName')

I'm asking because sometimes I have to run code using dbstop if caught error.


hi cyberfrog

unfortunately, this is not possible...
you have to create the complete CELL in your loop and append it afterwards using the -fa option...
note: you could use the -Lc option to create the column labels at write-time...



So I looked at the part you mentioned and it doesn’t do what I want. I’m trying to avoid xlswrite because I have a mix of strings and numbers. Is there any way to make cprintf do the actual file output but without any spaces for padding?


I may well have missed that part. It’s a pretty long file and searching it for “tab” or “space” brings up a lot of hits. I’ll peruse that part. Thanks!


hi yonatan

did you look at the last section in CPDOC(?)...
--- input to xlswrite

it shows how to use the content of
to create (for instance) excel data



This function is great for quickly and easily printing a cell array to a text file without worrying too much about format strings. But I can’t figure out how to prevent it from doing any padding with spaces. I’d like to output to a strictly tab-delimited file. I’ll probably be reading said file into excel or SPSS, so I don’t need the spaces for alignment, but I can’t figure out any way to leave them out entirely. (..., 'ntab', 1) isn’t doing it. Am I missing something, or is that functionality absent.



one of the most useful and definitely *the* best well made/structured/documented entry in the FEX



I quick question. If I create a column of numbers to reference the rows in my table, I would like to associate those numbers with a hyperlink. While I can perform this operation, the spacing/formatting of the column labels becomes quite ugly. Is there a simple solution?

By the way, I really enjoy using your function. It's great!


Hal 9000


1) you show the input to FPRINTF; how do you call CPRINTF?
2) the error message indicates that some input values are not defined(?)
can you clarify?

Gordon Ariho

I have tried cprintf on both release 7.5 and 7.8 but i get 'undefined function or variable...' I have tried to use fprintf but it disorganises the matrix

fprintf('Distance \t Path_loss \t Rx_Power \t SNR \n');fprintf('%f \t %f \t %f \t %f \n',K)

Y Mehta


MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!