4.0

4.0 | 1 rating Rate this file 104 downloads (last 30 days) File Size: 1.57 KB File ID: #10006

mtx2charcell

by Gerald Dalley

 

15 Feb 2006 (Updated 21 Feb 2006)

No BSD License  

Converts a matrix of numbers into a cell array of strings.

Download Now | Watch this File

File Information
Description

%charcell = mtx2charcell(mtx)
% Converts a matrix of numbers into a cell array of strings of the same
% dimensionality as MTX.
%charcell = mtx2charcell(mtx,fmt)
% Uses the format string FMT (see SPRINTF for details).
%
%This function serves as a shorthand for using the built-in NUM2CELL,
%NUM2STR, and RESHAPE commands together (thanks Urs Schwarz!)
%
%EXAMPLES:
% mtx2charcell([0 1 2]) --> {'0.000000', '1.000000', '2.000000'}
% mtx2charcell([0 1 2], '%d') --> {'0', '1', '2'}
%
%This function can be handy to use in conjunction with the author's JOIN
%function (also available on Matlab Central). For example,
% join('-', mtx2charcell([0 1 2], '%d')) --> '0-1-2'
%
%CHANGES:
% 2006-02-16 6pm: even better performance enhancement, thanks to Urs Schwarz
% 2006-02-16 afternoon: performance enhancement thanks to Jiro Doke.

MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
16 Feb 2006 Jiro Doke

Nice, I've had a need for something like this. But the loop can be sped up by vectorizing and using strread:

%-----------------------
mtx=[1:100000];

fmt='%8.2f';

tic;
charcell1=cell(size(mtx));
for i=1:numel(mtx)
  charcell{i}=sprintf(fmt, mtx(i));
end
toc;

tic;
charcell2=strread(sprintf([fmt ','],mtx),'%s',...
  'delimiter',',','whitespace','');
charcell2=reshape(charcell2,size(mtx));
toc;

%-----------------------

16 Feb 2006 Jiro Doke

Sorry, there's a typo in my example. charcell inside the for-loop should read

charcell1{i}=sprintf(fmt, mtx(i));

% forgot the '1' after 'charcell'.

16 Feb 2006 urs (us) schwarz

loop-equivalent for the well known siblings NUM2STR and NUM2CELL, eg,

x=0:2;
num2cell(num2str(x','%5.1d'),2)'
% ans = '0' '1' '2'

pointers to these genuine functions should be provided in the help section
us

28 Feb 2006 Jiro Doke

There's a difference in the output using US's method. For example, try the following using my and Us's method:

mtx = [1 2 3];
fmt = '%4d';

Notice that num2str strips leading spaces. You need to choose a method based on what you want the code to do.

Please login to add a comment or rating.
Updates
17 Feb 2006

Added optimization inspired by Jiro Doke's suggestion.

17 Feb 2006

Changed the implementation to expand on a suggestion by Urs Schwarz that cleans it up and keeps it fast.

17 Feb 2006

Changed the implementation to expand on a suggestion by Urs Schwarz that cleans it up and keeps it fast.

17 Feb 2006

Changed the implementation to expand on a suggestion by Urs Schwarz that cleans it up and keeps it fast.

21 Feb 2006

Removed boilerplate copyright.

Tag Activity for this File
Tag Applied By Date/Time
strings Gerald Dalley 22 Oct 2008 08:15:55
string cell number Gerald Dalley 22 Oct 2008 08:15:55
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com