Thread Subject: cell array to char array

Subject: cell array to char array

From: mohammad movassat

Date: 1 Jul, 2009 18:03:02

Message: 1 of 11

I have a g<1*10> cell array. I need to convert it to 1*10 char array. here is what I'm trying:

E=char(g);

and here is the error I get:

??? Error using ==> char
Cell elements must be character arrays.

Error in ==> looptest at 19
E=char(g);

any suggestion?

thank you,

Subject: cell array to char array

From: Jos

Date: 1 Jul, 2009 18:32:02

Message: 2 of 11

"mohammad movassat" <movassat@gmail.com> wrote in message <h2g8cm$pqa$1@fred.mathworks.com>...
> I have a g<1*10> cell array. I need to convert it to 1*10 char array. here is what I'm trying:
>
> E=char(g);
>
> and here is the error I get:
>
> ??? Error using ==> char
> Cell elements must be character arrays.
>
> Error in ==> looptest at 19
> E=char(g);
>
> any suggestion?
>
> thank you,

Clearly, at least one of the elements is NOT a character array. For you to find out which and why ...

Jos

Subject: cell array to char array

From: us

Date: 1 Jul, 2009 18:33:02

Message: 3 of 11

"mohammad movassat" <movassat@gmail.com> wrote in message <h2g8cm$pqa$1@fred.mathworks.com>...
> I have a g<1*10> cell array. I need to convert it to 1*10 char array. here is what I'm trying:
>
> E=char(g);
>
> and here is the error I get:
>
> ??? Error using ==> char
> Cell elements must be character arrays.
>
> Error in ==> looptest at 19
> E=char(g);
>
> any suggestion?
>
> thank you,

well, the syntax is correct - but the error message says it all:
- one of your CELL members is NOT a character...

     c={'a',1,'b'};
     char(c)
%{
??? Error using ==> char
Cell elements must be character arrays.
%}

us

Subject: cell array to char array

From: mohammad movassat

Date: 1 Jul, 2009 18:50:17

Message: 4 of 11

"us " <us@neurol.unizh.ch> wrote in message <h2ga4u$huu$1@fred.mathworks.com>...
> "mohammad movassat" <movassat@gmail.com> wrote in message <h2g8cm$pqa$1@fred.mathworks.com>...
> > I have a g<1*10> cell array. I need to convert it to 1*10 char array. here is what I'm trying:
> >
> > E=char(g);
> >
> > and here is the error I get:
> >
> > ??? Error using ==> char
> > Cell elements must be character arrays.
> >
> > Error in ==> looptest at 19
> > E=char(g);
> >
> > any suggestion?
> >
> > thank you,
>
> well, the syntax is correct - but the error message says it all:
> - one of your CELL members is NOT a character...
>
> c={'a',1,'b'};
> char(c)
> %{
> ??? Error using ==> char
> Cell elements must be character arrays.
> %}
>
> us

thanks for your responses, in cell g<> I have some symbolic equations and when I want to convert them to char, I get the error I said. Here is the program I wrote to generate g<1*2> and convert it to char:

a = sym('a');
x = sym(zeros(1, 2));

for k = 1:2,
    x(k) = sym(sprintf('x%d', k));
end

f=a^3*x(1)+a^2*x(2)+2*a;

h=diff(f,a);
g = cell(1, 2);
teta=1;
for k = 1:2
    g{k} = subs(h, a, teta);
    teta=teta+1;
end
E=char(g);

the reason I have to convert them to char is that I need to write them in m-file as function and then call fsolve to solve them (solve two equations with two unknowns x(1) and x(2)). So what do you think? please let me know.

Thank you

Subject: cell array to char array

From: us

Date: 1 Jul, 2009 19:27:01

Message: 5 of 11

"mohammad movassat" <movassat@gmail.com> wrote in message <h2gb59$n42$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <h2ga4u$huu$1@fred.mathworks.com>...
> > "mohammad movassat" <movassat@gmail.com> wrote in message <h2g8cm$pqa$1@fred.mathworks.com>...
> > > I have a g<1*10> cell array. I need to convert it to 1*10 char array. here is what I'm trying:
> > >
> > > E=char(g);
> > >
> > > and here is the error I get:
> > >
> > > ??? Error using ==> char
> > > Cell elements must be character arrays.
> > >
> > > Error in ==> looptest at 19
> > > E=char(g);
> > >
> > > any suggestion?
> > >
> > > thank you,
> >
> > well, the syntax is correct - but the error message says it all:
> > - one of your CELL members is NOT a character...
> >
> > c={'a',1,'b'};
> > char(c)
> > %{
> > ??? Error using ==> char
> > Cell elements must be character arrays.
> > %}
> >
> > us
>
> thanks for your responses, in cell g<> I have some symbolic equations and when I want to convert them to char, I get the error I said. Here is the program I wrote to generate g<1*2> and convert it to char:
>
> a = sym('a');
> x = sym(zeros(1, 2));
>
> for k = 1:2,
> x(k) = sym(sprintf('x%d', k));
> end
>
> f=a^3*x(1)+a^2*x(2)+2*a;
>
> h=diff(f,a);
> g = cell(1, 2);
> teta=1;
> for k = 1:2
> g{k} = subs(h, a, teta);
> teta=teta+1;
> end
> E=char(g);
>
> the reason I have to convert them to char is that I need to write them in m-file as function and then call fsolve to solve them (solve two equations with two unknowns x(1) and x(2)). So what do you think? please let me know.

interesting problem since the obvious approach does not work in this case

     r=char([g{:}])
% r = matrix([[3*x1 + 2*x2 + 2,12*x1 + 4*x2 + 2]])
% the sym-engine concatenates the sym-objs BEFORE the CHAR conversion
% takes place
% thus, another (more tedious) solution

     r=char(cellfun(@(x) char(x),g,'uni',false).')
%{
% r =
          3*x1 + 2*x2 + 2
          12*x1 + 4*x2 + 2
%}

us

Subject: cell array to char array

From: Oleg Komarov

Date: 1 Jul, 2009 19:27:01

Message: 6 of 11

"mohammad movassat" <movassat@gmail.com> wrote in message <h2gb59$n42$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <h2ga4u$huu$1@fred.mathworks.com>...
> > "mohammad movassat" <movassat@gmail.com> wrote in message <h2g8cm$pqa$1@fred.mathworks.com>...
> > > I have a g<1*10> cell array. I need to convert it to 1*10 char array. here is what I'm trying:
> > >
> > > E=char(g);
> > >
> > > and here is the error I get:
> > >
> > > ??? Error using ==> char
> > > Cell elements must be character arrays.
> > >
> > > Error in ==> looptest at 19
> > > E=char(g);
> > >
> > > any suggestion?
> > >
> > > thank you,
> >
> > well, the syntax is correct - but the error message says it all:
> > - one of your CELL members is NOT a character...
> >
> > c={'a',1,'b'};
> > char(c)
> > %{
> > ??? Error using ==> char
> > Cell elements must be character arrays.
> > %}
> >
> > us
>
> thanks for your responses, in cell g<> I have some symbolic equations and when I want to convert them to char, I get the error I said. Here is the program I wrote to generate g<1*2> and convert it to char:
>
> a = sym('a');
> x = sym(zeros(1, 2));
>
> for k = 1:2,
> x(k) = sym(sprintf('x%d', k));
> end
>
> f=a^3*x(1)+a^2*x(2)+2*a;
>
> h=diff(f,a);
> g = cell(1, 2);
> teta=1;
> for k = 1:2
> g{k} = subs(h, a, teta);
> teta=teta+1;
> end
> E=char(g);
>
> the reason I have to convert them to char is that I need to write them in m-file as function and then call fsolve to solve them (solve two equations with two unknowns x(1) and x(2)). So what do you think? please let me know.
>
> Thank you

E=char(g{:});

Oleg

Subject: cell array to char array

From: mohammad movassat

Date: 2 Jul, 2009 15:55:03

Message: 7 of 11

"us " <us@neurol.unizh.ch> wrote in message <h2gda5$dhl$1@fred.mathworks.com>...
> "mohammad movassat" <movassat@gmail.com> wrote in message <h2gb59$n42$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <h2ga4u$huu$1@fred.mathworks.com>...
> > > "mohammad movassat" <movassat@gmail.com> wrote in message <h2g8cm$pqa$1@fred.mathworks.com>...
> > > > I have a g<1*10> cell array. I need to convert it to 1*10 char array. here is what I'm trying:
> > > >
> > > > E=char(g);
> > > >
> > > > and here is the error I get:
> > > >
> > > > ??? Error using ==> char
> > > > Cell elements must be character arrays.
> > > >
> > > > Error in ==> looptest at 19
> > > > E=char(g);
> > > >
> > > > any suggestion?
> > > >
> > > > thank you,
> > >
> > > well, the syntax is correct - but the error message says it all:
> > > - one of your CELL members is NOT a character...
> > >
> > > c={'a',1,'b'};
> > > char(c)
> > > %{
> > > ??? Error using ==> char
> > > Cell elements must be character arrays.
> > > %}
> > >
> > > us
> >
> > thanks for your responses, in cell g<> I have some symbolic equations and when I want to convert them to char, I get the error I said. Here is the program I wrote to generate g<1*2> and convert it to char:
> >
> > a = sym('a');
> > x = sym(zeros(1, 2));
> >
> > for k = 1:2,
> > x(k) = sym(sprintf('x%d', k));
> > end
> >
> > f=a^3*x(1)+a^2*x(2)+2*a;
> >
> > h=diff(f,a);
> > g = cell(1, 2);
> > teta=1;
> > for k = 1:2
> > g{k} = subs(h, a, teta);
> > teta=teta+1;
> > end
> > E=char(g);
> >
> > the reason I have to convert them to char is that I need to write them in m-file as function and then call fsolve to solve them (solve two equations with two unknowns x(1) and x(2)). So what do you think? please let me know.
>
> interesting problem since the obvious approach does not work in this case
>
> r=char([g{:}])
> % r = matrix([[3*x1 + 2*x2 + 2,12*x1 + 4*x2 + 2]])
> % the sym-engine concatenates the sym-objs BEFORE the CHAR conversion
> % takes place
> % thus, another (more tedious) solution
>
> r=char(cellfun(@(x) char(x),g,'uni',false).')
> %{
> % r =
> 3*x1 + 2*x2 + 2
> 12*x1 + 4*x2 + 2
> %}
>
> us

Hi,

thanks for your response, I ran it with
 r=char(cellfun(@(x) char(x),g,'uni',false).'),

here is the whole code:
clear;
clc;
a = sym('a');
x = sym(zeros(1, 2));

for k = 1:2,
    x(k) = sym(sprintf('x%d', k));
end

f=a^3*x(1)+a^2*x(2)+2*a;

h=diff(f,a);
g = cell(1, 2);
teta=1;
for k = 1:2
    g{k} = subs(h, a, teta);
    teta=teta+1;
end
 r=char(cellfun(@(x) char(x),g,'uni',false).')


and I got this error:

??? Too many inputs.

Error in ==> looptest at 19
 r=char(cellfun(@(x) char(x),g,'uni',false).')

any suggestions?

thank you,

Mohammad

Subject: cell array to char array

From: us

Date: 2 Jul, 2009 16:13:02

Message: 8 of 11

"mohammad movassat"
> here is the whole code:
> clear;
> clc;
> a = sym('a');
> x = sym(zeros(1, 2));
>
> for k = 1:2,
> x(k) = sym(sprintf('x%d', k));
> end
>
> f=a^3*x(1)+a^2*x(2)+2*a;
>
> h=diff(f,a);
> g = cell(1, 2);
> teta=1;
> for k = 1:2
> g{k} = subs(h, a, teta);
> teta=teta+1;
> end
> r=char(cellfun(@(x) char(x),g,'uni',false).')

if i copy/paste those lines above into the command window, it runs just fine...
hence, you're doing something in your program that you don't show...

us

Subject: cell array to char array

From: us

Date: 2 Jul, 2009 16:14:02

Message: 9 of 11

"mohammad movassat"
> thanks for your response, I ran it with
> r=char(cellfun(@(x) char(x),g,'uni',false).'),
> here is the whole code:
> clear;
> clc;
> a = sym('a');
> x = sym(zeros(1, 2));
>
> for k = 1:2,
> x(k) = sym(sprintf('x%d', k));
> end
>
> f=a^3*x(1)+a^2*x(2)+2*a;
>
> h=diff(f,a);
> g = cell(1, 2);
> teta=1;
> for k = 1:2
> g{k} = subs(h, a, teta);
> teta=teta+1;
> end
> r=char(cellfun(@(x) char(x),g,'uni',false).')
> and I got this error:
> ??? Too many inputs.
> Error in ==> looptest at 19
> r=char(cellfun(@(x) char(x),g,'uni',false).')

if i copy/paste those lines above into the command window, it runs just fine...
hence, you're doing something in your program that you don't show...

us

Subject: cell array to char array

From: Steven Lord

Date: 2 Jul, 2009 16:54:48

Message: 10 of 11


"mohammad movassat" <movassat@gmail.com> wrote in message
news:h2il8n$si8$1@fred.mathworks.com...

*snip*

> thanks for your response, I ran it with
> r=char(cellfun(@(x) char(x),g,'uni',false).'),
>
> here is the whole code:
> clear;
> clc;
> a = sym('a');
> x = sym(zeros(1, 2));
>
> for k = 1:2,
> x(k) = sym(sprintf('x%d', k));
> end
>
> f=a^3*x(1)+a^2*x(2)+2*a;
>
> h=diff(f,a);
> g = cell(1, 2);
> teta=1;
> for k = 1:2
> g{k} = subs(h, a, teta);
> teta=teta+1;
> end
> r=char(cellfun(@(x) char(x),g,'uni',false).')
>
>
> and I got this error:
>
> ??? Too many inputs.
>
> Error in ==> looptest at 19
> r=char(cellfun(@(x) char(x),g,'uni',false).')
>
> any suggestions?

I suspect you have a version of MATLAB prior to the introduction of the
version of CELLFUN that accepts arbitrary function handles. If so, just use
a FOR loop -- you're looping over a small number of elements of a cell
array, so it should be reasonably fast.

--
Steve Lord
slord@mathworks.com

Subject: cell array to char array

From: mohammad movassat

Date: 2 Jul, 2009 17:10:03

Message: 11 of 11

"Steven Lord" <slord@mathworks.com> wrote in message <h2ion9$qak$1@fred.mathworks.com>...
>
> "mohammad movassat" <movassat@gmail.com> wrote in message
> news:h2il8n$si8$1@fred.mathworks.com...
>
> *snip*
>
> > thanks for your response, I ran it with
> > r=char(cellfun(@(x) char(x),g,'uni',false).'),
> >
> > here is the whole code:
> > clear;
> > clc;
> > a = sym('a');
> > x = sym(zeros(1, 2));
> >
> > for k = 1:2,
> > x(k) = sym(sprintf('x%d', k));
> > end
> >
> > f=a^3*x(1)+a^2*x(2)+2*a;
> >
> > h=diff(f,a);
> > g = cell(1, 2);
> > teta=1;
> > for k = 1:2
> > g{k} = subs(h, a, teta);
> > teta=teta+1;
> > end
> > r=char(cellfun(@(x) char(x),g,'uni',false).')
> >
> >
> > and I got this error:
> >
> > ??? Too many inputs.
> >
> > Error in ==> looptest at 19
> > r=char(cellfun(@(x) char(x),g,'uni',false).')
> >
> > any suggestions?
>
> I suspect you have a version of MATLAB prior to the introduction of the
> version of CELLFUN that accepts arbitrary function handles. If so, just use
> a FOR loop -- you're looping over a small number of elements of a cell
> array, so it should be reasonably fast.
>
> --
> Steve Lord
> slord@mathworks.com
>


Thank you. I also think that the problem comes from different versions we are using.

Regarding using FOR, my problem is that it does not accept r(k)=char(...). How would you write it using a FOR loop?

Thank you,
Mohammad

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
char us 1 Jul, 2009 15:29:10
cellfun us 1 Jul, 2009 15:29:10
sym us 1 Jul, 2009 15:29:10
syntax us 1 Jul, 2009 14:34:13
code us 1 Jul, 2009 14:34:13
rssFeed for this Thread

Contact us at files@mathworks.com