Got Questions? Get Answers.
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:
Blanks in a string from a cell array

Subject: Blanks in a string from a cell array

From: Philipp Steffen

Date: 4 Nov, 2010 16:29:06

Message: 1 of 9

Hi all,

i am stuck with the following problem:

I want to get a struct (files.F) that contains strings composed of the character 'C' and a number from the struct Param.Cells.
If i use the following code I get a blank in between the character "C" and the one-digit numbers but not for the number 10.

Param.Cells = [6 7 8 9 10]';
n=1:numel(Param.Cells);
files.F(n) = cellstr(strcat('C', num2str(Param.Cells)));

The result looks like:
C 6
C 7
C 8
C 9
C10

I already tried strtrim but this doesnt work. Do you have any hints how I can get rid of the blanks?

Thanks a lot in advance,

Philipp

Subject: Blanks in a string from a cell array

From: Sean

Date: 4 Nov, 2010 16:44:04

Message: 2 of 9

"Philipp Steffen" <philipp.steffen@imba.oeaw.ac.at> wrote in message <iaun0i$luo$1@fred.mathworks.com>...
> Hi all,
>
> i am stuck with the following problem:
>
> I want to get a struct (files.F) that contains strings composed of the character 'C' and a number from the struct Param.Cells.
> If i use the following code I get a blank in between the character "C" and the one-digit numbers but not for the number 10.
>
> Param.Cells = [6 7 8 9 10]';
> n=1:numel(Param.Cells);
> files.F(n) = cellstr(strcat('C', num2str(Param.Cells)));
>
> The result looks like:
> C 6
> C 7
> C 8
> C 9
> C10
>
> I already tried strtrim but this doesnt work. Do you have any hints how I can get rid of the blanks?
>
> Thanks a lot in advance,
>
> Philipp

help isspace
str(isspace(str)) = [];

Subject: Blanks in a string from a cell array

From: Jan Simon

Date: 4 Nov, 2010 17:03:04

Message: 3 of 9

Dear Philipp,

c = [6 7 8 9 10]';
F = cellstr(strcat('C', num2str(Param.Cells, '%.1d')));

>> F = {'C6', 'C7', 'C8', 'C9', 'C10'}

Jan

Subject: Blanks in a string from a cell array

From: Philipp Steffen

Date: 4 Nov, 2010 17:23:05

Message: 4 of 9

Dear Jan,
unfortunately your code gives also unwanted blanks (at least on my comuputer). I did the following:

c = [6 7 8 9 10]';
F = cellstr(strcat('C', num2str(c, '%.1d')));

>> F
F =
    'C 6'
    'C 7'
    'C 8'
    'C 9'
    'C10'

Would you please check wether thats the same on you computer or am i doing something wrong?

Best,
Philipp

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <iaup08$4uu$1@fred.mathworks.com>...
> Dear Philipp,
>
> c = [6 7 8 9 10]';
> F = cellstr(strcat('C', num2str(Param.Cells, '%.1d')));
>
> >> F = {'C6', 'C7', 'C8', 'C9', 'C10'}
>
> Jan

Subject: Blanks in a string from a cell array

From: Philipp Steffen

Date: 4 Nov, 2010 17:27:05

Message: 5 of 9

Hi Sean,
thank for the reply. Unfortunately your solution does not seam to work in the situation I described, where I am doing the operation on the cell array and structure or do I get something wrong here? Using isspace I would have to go through a loop don't I?
Best,
Philipp

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iaunsk$k2h$1@fred.mathworks.com>...
> "Philipp Steffen" <philipp.steffen@imba.oeaw.ac.at> wrote in message <iaun0i$luo$1@fred.mathworks.com>...
> > Hi all,
> >
> > i am stuck with the following problem:
> >
> > I want to get a struct (files.F) that contains strings composed of the character 'C' and a number from the struct Param.Cells.
> > If i use the following code I get a blank in between the character "C" and the one-digit numbers but not for the number 10.
> >
> > Param.Cells = [6 7 8 9 10]';
> > n=1:numel(Param.Cells);
> > files.F(n) = cellstr(strcat('C', num2str(Param.Cells)));
> >
> > The result looks like:
> > C 6
> > C 7
> > C 8
> > C 9
> > C10
> >
> > I already tried strtrim but this doesnt work. Do you have any hints how I can get rid of the blanks?
> >
> > Thanks a lot in advance,
> >
> > Philipp
>
> help isspace
> str(isspace(str)) = [];

Subject: Blanks in a string from a cell array

From: Sean

Date: 4 Nov, 2010 17:37:03

Message: 6 of 9

"Philipp Steffen" <philipp.steffen@imba.oeaw.ac.at> wrote in message <iauqd9$a0h$1@fred.mathworks.com>...
> Hi Sean,
> thank for the reply. Unfortunately your solution does not seam to work in the situation I described, where I am doing the operation on the cell array and structure or do I get something wrong here? Using isspace I would have to go through a loop don't I?
> Best,
> Philipp
>

Yes, using CELLFUN. A modification to Jan's method is probably a better solution.

Subject: Blanks in a string from a cell array

From: Jan Simon

Date: 4 Nov, 2010 19:40:08

Message: 7 of 9

Dear Philipp,

> unfortunately your code gives also unwanted blanks (at least on my comuputer). I did the following:
>
> c = [6 7 8 9 10]';
> F = cellstr(strcat('C', num2str(c, '%.1d')));
>
> >> F
> F =
> 'C 6' ,...

NUM2STR does not insert blanks in Matlab 6.5, but it does in Matlab 2009a. So simply downgrade to 6.5 to solve the problem. ;-)

Anyhow, NUM2STR contains a FOR loop also. This looks less professional, but is 3 times faster then the CELLSTR(STRCAT(NUM2STR))) approach:
   c = [6 7 8 9 10]';
   F = cell(size(c));
   for i = 1:numelF
     F{i} = sprintf('C%d', c(i));
   end

Btw, I tried this also:
  F = cellstr(num2str(c, 'C%d'))
 >> F = {' C7', ' C8', ' C9', 'C10'}
 % Leading spaces before C for 7,8,9 !
I'm not a fan of NUM2STR.

Kind regards, Jan

Subject: Blanks in a string from a cell array

From: Walter Roberson

Date: 4 Nov, 2010 19:57:02

Message: 8 of 9

On 10-11-04 11:29 AM, Philipp Steffen wrote:

> I want to get a struct (files.F) that contains strings composed of the
> character 'C' and a number from the struct Param.Cells.
> If i use the following code I get a blank in between the character "C"
> and the one-digit numbers but not for the number 10.
>
> Param.Cells = [6 7 8 9 10]';
> n=1:numel(Param.Cells);
> files.F(n) = cellstr(strcat('C', num2str(Param.Cells)));
>
> The result looks like:
> C 6
> C 7
> C 8
> C 9
> C10

 >> c = [6 7 8 9 10].';
 >> cellstr(num2str(c,'C%-d'))
ans =
     'C6'
     'C7'
     'C8'
     'C9'
     'C10'

Subject: Blanks in a string from a cell array

From: Philipp Steffen

Date: 5 Nov, 2010 08:06:04

Message: 9 of 9

Hi Jan und Walter,

thanks to both of you.
Jan: i didn't realize that 2009b num2str works differently then before.
Walter: this seems to be the easiest way for me. I will use that.
Thanks a lot!

Philipp

Walter Roberson <roberson@hushmail.com> wrote in message <iav36m$3vj$1@canopus.cc.umanitoba.ca>...
> On 10-11-04 11:29 AM, Philipp Steffen wrote:
>
> > I want to get a struct (files.F) that contains strings composed of the
> > character 'C' and a number from the struct Param.Cells.
> > If i use the following code I get a blank in between the character "C"
> > and the one-digit numbers but not for the number 10.
> >
> > Param.Cells = [6 7 8 9 10]';
> > n=1:numel(Param.Cells);
> > files.F(n) = cellstr(strcat('C', num2str(Param.Cells)));
> >
> > The result looks like:
> > C 6
> > C 7
> > C 8
> > C 9
> > C10
>
> >> c = [6 7 8 9 10].';
> >> cellstr(num2str(c,'C%-d'))
> ans =
> 'C6'
> 'C7'
> 'C8'
> 'C9'
> 'C10'

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