Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
use of num2str and cellstr

Subject: use of num2str and cellstr

From: Wendy

Date: 26 Mar, 2011 20:34:04

Message: 1 of 3

Hi all,

I want to read a list of numbers from a tab-delimited file as strings, but I have some problems converting from numbers to cell strings. For example,

A = [123; 1234; 12345];
A = cellstr(num2str(A))

A =

    ' 123'
    ' 1234'
    '12345'

I was wondering if there is way that I can read a numeric vector as cell strings, but without the space before the 1st and the 2nd element, ie., I want to the output to be
A =
      '123'
     '1234'
    '12345'

Thank you in advance,
Wendy

Subject: use of num2str and cellstr

From: Nasser M. Abbasi

Date: 26 Mar, 2011 20:42:19

Message: 2 of 3

On 3/26/2011 1:34 PM, Wendy wrote:
> Hi all,
>
> I want to read a list of numbers from a tab-delimited file as strings,
> but I have some problems converting from numbers to cell strings. For example,
>
> A = [123; 1234; 12345];
> A = cellstr(num2str(A))
>
> A =
>
> ' 123'
> ' 1234'
> '12345'
>
> I was wondering if there is way that I can read a numeric vector as cell strings, but without the
> space before the 1st and the 2nd element, ie., I want to the output to be
> A =
>
> '123'
> '1234'
> '12345'
>

Why are you not using textscan to read your data?

For your other question, may be:

----------------------------------
A = [123; 1234; 12345];
B=arrayfun(@(i) num2str(A(i)),1:length(A),'UniformOutput',false);
----------------------
EDU>> B
B =
     '123' '1234' '12345'

--Nasser

Subject: use of num2str and cellstr

From: Wayne King

Date: 26 Mar, 2011 20:57:04

Message: 3 of 3

"Wendy " <wlq121@gmail.com> wrote in message <imlijs$qat$1@fred.mathworks.com>...
> Hi all,
>
> I want to read a list of numbers from a tab-delimited file as strings, but I have some problems converting from numbers to cell strings. For example,
>
> A = [123; 1234; 12345];
> A = cellstr(num2str(A))
>
> A =
>
> ' 123'
> ' 1234'
> '12345'
>
> I was wondering if there is way that I can read a numeric vector as cell strings, but without the space before the 1st and the 2nd element, ie., I want to the output to be
> A =
> '123'
> '1234'
> '12345'
>
> Thank you in advance,
> Wendy

Hi Wendy, One thing you can do is to flip your strings and then remove the trailing white spaces and then flip them back.

A = [123; 1234; 12345];
A = cellstr(deblank(fliplr(num2str(A))));
A = cellfun(@fliplr,A,'UniformOutput',false);

Wayne

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us