Discussion:
cell array to char array
(too old to reply)
mohammad movassat
2009-07-01 18:03:02 UTC
Permalink
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,
Jos
2009-07-01 18:32:02 UTC
Permalink
Post by mohammad movassat
E=char(g);
??? 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
us
2009-07-01 18:33:02 UTC
Permalink
Post by mohammad movassat
E=char(g);
??? 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
mohammad movassat
2009-07-01 18:50:17 UTC
Permalink
Post by us
Post by mohammad movassat
E=char(g);
??? Error using ==> char
Cell elements must be character arrays.
Error in ==> looptest at 19
E=char(g);
any suggestion?
thank you,
- 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
us
2009-07-01 19:27:01 UTC
Permalink
Post by mohammad movassat
Post by us
Post by mohammad movassat
E=char(g);
??? Error using ==> char
Cell elements must be character arrays.
Error in ==> looptest at 19
E=char(g);
any suggestion?
thank you,
- 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
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
mohammad movassat
2009-07-02 15:55:03 UTC
Permalink
Post by us
Post by mohammad movassat
Post by us
Post by mohammad movassat
E=char(g);
??? Error using ==> char
Cell elements must be character arrays.
Error in ==> looptest at 19
E=char(g);
any suggestion?
thank you,
- 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
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 =
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
us
2009-07-02 16:13:02 UTC
Permalink
"mohammad movassat"
Post by mohammad movassat
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
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
us
2009-07-02 16:14:02 UTC
Permalink
"mohammad movassat"
Post by mohammad movassat
thanks for your response, I ran it with
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
??? Too many inputs.
Error in ==> looptest at 19
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
Steven Lord
2009-07-02 16:54:48 UTC
Permalink
"mohammad movassat" <***@gmail.com> wrote in message news:h2il8n$si8$***@fred.mathworks.com...

*snip*
Post by mohammad movassat
thanks for your response, I ran it with
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
??? Too many inputs.
Error in ==> looptest at 19
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
***@mathworks.com
mohammad movassat
2009-07-02 17:10:03 UTC
Permalink
Post by Steven Lord
*snip*
Post by mohammad movassat
thanks for your response, I ran it with
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
??? Too many inputs.
Error in ==> looptest at 19
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
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

Oleg Komarov
2009-07-01 19:27:01 UTC
Permalink
Post by mohammad movassat
Post by us
Post by mohammad movassat
E=char(g);
??? Error using ==> char
Cell elements must be character arrays.
Error in ==> looptest at 19
E=char(g);
any suggestion?
thank you,
- 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
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
Loading...