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:
Conversion to cell from double is not possible - Error

Subject: Conversion to cell from double is not possible - Error

From: michael caro

Date: 4 May, 2010 22:02:16

Message: 1 of 11

Can someone help me with this problem. I'll post the instructions, and
the code I've created.

Given the following equation: y=1/ sqrt [1+(αt)^2]

where 0 ≤ α ≤ 2 and 0 ≤ t ≤ 5. We would like to see how the plot of y
vs. t changes as the value of the parameter α changes. This is to be
done in the following manner:

Create an array of values for t using the range specified in
increments of 0.05 and an array for α (alpha) over the range specified
in increments of 0.5.

t=0:0.05:5;
a=0:0.5:2;

Find the number of values in the array, alpha, using the numel command
(see MATLAB help if you don’t remember it). Create an array Y to store
values to be plotted. The variable Y should have as many rows as there
are values of alpha and as many columns as there are values of t.

nt=numel(t);
na=numel(a);
Y=cell(na,nt);

Create a for loop that uses an indexing value from 1 to the number
values of alpha, found in (2) by the numel command. For each value of
‘α’, calculate the y values that correspond to the t values given the
equation from above with a vectorized equation. Save your results for
each value of alpha in each corresponding row of Y.

for i = 1:na
    Y(a,:)=1./sqrt(1+(t.*a(i)).^2)
end

So i get the error, " Conversion to cell from double is not possible "

I really appreciate the help.

Subject: Conversion to cell from double is not possible - Error

From: Nathan

Date: 4 May, 2010 22:09:11

Message: 2 of 11

On May 4, 3:02 pm, michael caro <mcaro1...@gmail.com> wrote:
> Can someone help me with this problem. I'll post the instructions, and
> the code I've created.
>
> Given the following equation: y=1/ sqrt [1+(αt)^2]
>
> where 0 ≤ α ≤ 2 and 0 ≤ t ≤ 5. We would like to see how the plot of y
> vs. t changes as the value of the parameter α changes. This is to be
> done in the following manner:
>
> Create an array of values for t using the range specified in
> increments of 0.05 and an array for α (alpha) over the range specified
> in increments of 0.5.
>
> t=0:0.05:5;
> a=0:0.5:2;
>
> Find the number of values in the array, alpha, using the numel command
> (see MATLAB help if you don’t remember it). Create an array Y to store
> values to be plotted. The variable Y should have as many rows as there
> are values of alpha and as many columns as there are values of t.
>
> nt=numel(t);
> na=numel(a);
> Y=cell(na,nt);
>
> Create a for loop that uses an indexing value from 1 to the number
> values of alpha, found in (2) by the numel command. For each value of
> ‘α’, calculate the y values that correspond to the t values given the
> equation from above with a vectorized equation. Save your results for
> each value of alpha in each corresponding row of Y.
>
> for i = 1:na
>     Y(a,:)=1./sqrt(1+(t.*a(i)).^2)
> end
>
> So i get the error, " Conversion to cell from double is not possible "
>
> I really appreciate the help.

Why is Y being indexed by a?
Do you mean Y(i,:)?

If so:

for i = 1:na
    Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
end

Is that what you were looking for?

-Nathan

Subject: Conversion to cell from double is not possible - Error

From: Eric

Date: 4 May, 2010 22:17:04

Message: 3 of 11

You could just be lazy and let MATLAB handle your array sizing:

t=0:0.05:5;
a=0:0.5:2;
Y = [];
for i = 1:length(a)
    Y=[Y;1./sqrt(1+(t.*a(i)).^2)];
end

Subject: Conversion to cell from double is not possible - Error

From: michael caro

Date: 4 May, 2010 22:40:26

Message: 4 of 11

On May 4, 6:09 pm, Nathan <ngrec...@gmail.com> wrote:
> On May 4, 3:02 pm, michael caro <mcaro1...@gmail.com> wrote:
>
>
>
> > Can someone help me with this problem. I'll post the instructions, and
> > the code I've created.
>
> > Given the following equation: y=1/ sqrt [1+(αt)^2]
>
> > where 0 ≤ α ≤ 2 and 0 ≤ t ≤ 5. We would like to see how the plot of y
> > vs. t changes as the value of the parameter α changes. This is to be
> > done in the following manner:
>
> > Create an array of values for t using the range specified in
> > increments of 0.05 and an array for α (alpha) over the range specified
> > in increments of 0.5.
>
> > t=0:0.05:5;
> > a=0:0.5:2;
>
> > Find the number of values in the array, alpha, using the numel command
> > (see MATLAB help if you don’t remember it). Create an array Y to store
> > values to be plotted. The variable Y should have as many rows as there
> > are values of alpha and as many columns as there are values of t.
>
> > nt=numel(t);
> > na=numel(a);
> > Y=cell(na,nt);
>
> > Create a for loop that uses an indexing value from 1 to the number
> > values of alpha, found in (2) by the numel command. For each value of
> > ‘α’, calculate the y values that correspond to the t values given the
> > equation from above with a vectorized equation. Save your results for
> > each value of alpha in each corresponding row of Y.
>
> > for i = 1:na
> >     Y(a,:)=1./sqrt(1+(t.*a(i)).^2)
> > end
>
> > So i get the error, " Conversion to cell from double is not possible "
>
> > I really appreciate the help.
>
> Why is Y being indexed by a?
> Do you mean Y(i,:)?
>
> If so:
>
> for i = 1:na
>     Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
> end
>
> Is that what you were looking for?
>
> -Nathan

Thank you for picking up on the mistake but thats not exactly what I'm
looking for.

How can I change the loop so matlab doesnt try to convert it into a
double?
i'd like to create an array with 5 rows and 101 columns. the 5 rows
correspond to each different 'a' variable being evaluated with all of
the 't' variables, so i can then create a plot saying plot(t,Y)

thanks once again

Subject: Conversion to cell from double is not possible - Error

From: Walter Roberson

Date: 4 May, 2010 23:03:10

Message: 5 of 11

michael caro wrote:
> On May 4, 6:09 pm, Nathan <ngrec...@gmail.com> wrote:

>> If so:
>>
>> for i = 1:na
>> Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
>> end


> Thank you for picking up on the mistake but thats not exactly what I'm
> looking for.

> How can I change the loop so matlab doesnt try to convert it into a
> double?

Nathan already answered that. Go back and reexamine the line, and "note the
braces". His solution already prevents matlab from trying to convert into a
double.

> i'd like to create an array with 5 rows and 101 columns. the 5 rows
> correspond to each different 'a' variable being evaluated with all of
> the 't' variables, so i can then create a plot saying plot(t,Y)

If you want to be able to do it with a single plot() call like you show, then
you *do* want the values converted to double -- plot can handle a numeric Y
array with multiple rows, but it cannot handle cell arrays to represent the rows.

Subject: Conversion to cell from double is not possible - Error

From: michael caro

Date: 4 May, 2010 23:44:50

Message: 6 of 11

On May 4, 7:03 pm, Walter Roberson <rober...@hushmail.com> wrote:
> michael caro wrote:
> > On May 4, 6:09 pm, Nathan <ngrec...@gmail.com> wrote:
> >> If so:
>
> >> for i = 1:na
> >>     Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
> >> end
> > Thank you for picking up on the mistake but thats not exactly what I'm
> > looking for.
> > How can I change the loop so matlab doesnt try to convert it into a
> > double?
>
> Nathan already answered that. Go back and reexamine the line, and "note the
> braces". His solution already prevents matlab from trying to convert into a
> double.
>
> > i'd like to create an array with 5 rows and 101 columns. the 5 rows
> > correspond to each different 'a' variable being evaluated with all of
> > the 't' variables, so i can then create a plot saying plot(t,Y)
>
> If you want to be able to do it with a single plot() call like you show, then
> you *do* want the values converted to double -- plot can handle a numeric Y
> array with multiple rows, but it cannot handle cell arrays to represent the rows.

When I run the script with Nathan's suggestion, this is the output :
Y =

  Columns 1 through 5

    [1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]
    [1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]
    [1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]
    [1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]
    [1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]

for all columns.

Subject: Conversion to cell from double is not possible - Error

From: Matt Fig

Date: 5 May, 2010 00:05:22

Message: 7 of 11

You were close.


t=0:0.05:5;
a=0:0.5:2;
Y = zeros(numel(a),numel(t)); % No cell array needed.

for ii = 1:numel(a)
    Y(ii,:) = 1./sqrt(1+(a(ii)*t).^2);
end

plot(t,Y')
legend(cellstr(num2str(a')))

Subject: Conversion to cell from double is not possible - Error

From: Nathan

Date: 5 May, 2010 00:05:50

Message: 8 of 11

On May 4, 4:44 pm, michael caro <mcaro1...@gmail.com> wrote:
> On May 4, 7:03 pm, Walter Roberson <rober...@hushmail.com> wrote:
>
>
>
> > michael caro wrote:
> > > On May 4, 6:09 pm, Nathan <ngrec...@gmail.com> wrote:
> > >> If so:
>
> > >> for i = 1:na
> > >>     Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
> > >> end
> > > Thank you for picking up on the mistake but thats not exactly what I'm
> > > looking for.
> > > How can I change the loop so matlab doesnt try to convert it into a
> > > double?
>
> > Nathan already answered that. Go back and reexamine the line, and "note the
> > braces". His solution already prevents matlab from trying to convert into a
> > double.
>
> > > i'd like to create an array with 5 rows and 101 columns. the 5 rows
> > > correspond to each different 'a' variable being evaluated with all of
> > > the 't' variables, so i can then create a plot saying plot(t,Y)
>
> > If you want to be able to do it with a single plot() call like you show, then
> > you *do* want the values converted to double -- plot can handle a numeric Y
> > array with multiple rows, but it cannot handle cell arrays to represent the rows.
>
> When I run the script with Nathan's suggestion, this is the output :
> Y =
>
>   Columns 1 through 5
>
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>
> for all columns.

Just note: That is precisely what YOUR code does. All I did was get
rid of the cell to double conversion problem.
Also note that you asked for a 5x101 Y cell array, and that is what
you get.

Perhaps you should take a look at your code, go through step by step,
and determine what is wrong.

As Walter suggests, perhaps you DO want a double array Y. In this
case, initialize Y to be a double array, not a cell array.

t=0:0.05:5;
a=0:0.5:2;
nt=numel(t);
na=numel(a);
Y=zeros(na,nt); %allocate array
for i = 1:na
    Y(i,:)=1./sqrt(1+(t.*a(i)).^2);
end

-Nathan

Subject: Conversion to cell from double is not possible - Error

From: michael caro

Date: 5 May, 2010 00:34:08

Message: 9 of 11

On May 4, 8:05 pm, Nathan <ngrec...@gmail.com> wrote:
> On May 4, 4:44 pm, michael caro <mcaro1...@gmail.com> wrote:
>
>
>
> > On May 4, 7:03 pm, Walter Roberson <rober...@hushmail.com> wrote:
>
> > > michael caro wrote:
> > > > On May 4, 6:09 pm, Nathan <ngrec...@gmail.com> wrote:
> > > >> If so:
>
> > > >> for i = 1:na
> > > >>     Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
> > > >> end
> > > > Thank you for picking up on the mistake but thats not exactly what I'm
> > > > looking for.
> > > > How can I change the loop so matlab doesnt try to convert it into a
> > > > double?
>
> > > Nathan already answered that. Go back and reexamine the line, and "note the
> > > braces". His solution already prevents matlab from trying to convert into a
> > > double.
>
> > > > i'd like to create an array with 5 rows and 101 columns. the 5 rows
> > > > correspond to each different 'a' variable being evaluated with all of
> > > > the 't' variables, so i can then create a plot saying plot(t,Y)
>
> > > If you want to be able to do it with a single plot() call like you show, then
> > > you *do* want the values converted to double -- plot can handle a numeric Y
> > > array with multiple rows, but it cannot handle cell arrays to represent the rows.
>
> > When I run the script with Nathan's suggestion, this is the output :
> > Y =
>
> >   Columns 1 through 5
>
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
>
> > for all columns.
>
> Just note: That is precisely what YOUR code does. All I did was get
> rid of the cell to double conversion problem.
> Also note that you asked for a 5x101 Y cell array, and that is what
> you get.
>
> Perhaps you should take a look at your code, go through step by step,
> and determine what is wrong.
>
> As Walter suggests, perhaps you DO want a double array Y. In this
> case, initialize Y to be a double array, not a cell array.
>
> t=0:0.05:5;
> a=0:0.5:2;
> nt=numel(t);
> na=numel(a);
> Y=zeros(na,nt); %allocate array
> for i = 1:na
>     Y(i,:)=1./sqrt(1+(t.*a(i)).^2);
> end
>
> -Nathan

Nathan,

I really appreciate your help with this problem. I'm sorry if I seem a
little dense. The code works, your the man!

-Mike

Subject: Conversion to cell from double is not possible - Error

From: Walter Roberson

Date: 5 May, 2010 00:34:33

Message: 10 of 11

michael caro wrote:
> On May 4, 7:03 pm, Walter Roberson <rober...@hushmail.com> wrote:

>>> How can I change the loop so matlab doesnt try to convert it into a
>>> double?
>> Nathan already answered that. Go back and reexamine the line, and "note the
>> braces".

> When I run the script with Nathan's suggestion, this is the output :
> Y =
>
> Columns 1 through 5
>
> [1x101 double] [1x101 double] [1x101 double] [1x101
> double] [1x101 double]
> [1x101 double] [1x101 double] [1x101 double] [1x101

> for all columns.

Yup, exactly as I would expect from your code.

You asked to have the Matlab complaint about "Conversion to cell from double
is not possible" fixed, and you asked to have the loop be such that Matlab
doesn't try to convert the value to double, and the code Nathan provided
satisfies both conditions. Clearly the error about converting to cell from
double is gone, is it not? And in the loop, Matlab is not trying to convert
the value to double because it already knows that the value is a cell array,
which is what is required for the assignment into the array of cells, Y, to
succeed, so it doesn't have any reason to convert the computed cell array
value into double. The code is performing according to your specifications.

If you are talking about the fact that Y displays as a bunch of repetitions of
text that mention the word 'double', then Yes, that is how Matlab displays
every cell array entry that contains a double precision array, unless the
double precision array happens to contain only a single 1x1 value, in which
case that one value is displayed inside []. Matlab isn't converting anything
to double there: it is telling you about what is already in the cell array --
the double precision values are already there, without conversion.

If you want Matlab to use a different data type instead of double precision to
represent the values of your graph, then you are going to find you have a lot
of trouble getting a usable graph... unless, that is, you happen to choose
single precision instead. Your code appears likely to generate a large number
of values in the range 0 to 1: if you choose to represent those as any data
type other than double precision or single precision, then Matlab is going to
throw away the fractional parts, probably leaving you with just a bunch of
entries that are each either 0 or 1. You should be able to get a reasonable
graph with single precision -- if you want to give that a try, change the loop
from
Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
to
Y(i,:)={single(1./sqrt(1+(t.*a(i)).^2))} %NOTE THE BRACES ARE STILL THERE

You might as well go ahead and try it: the horse has to be beaten quite to
death before it can be turned into the right kind of glue.


When you are finished with that, you may wish to study my prior comment:

 >> If you want to be able to do it with a single plot() call like you show, then
 >> you *do* want the values converted to double -- plot can handle a numeric Y
 >> array with multiple rows, but it cannot handle cell arrays to represent
the rows.

Your Y array is *not* double, and I already told you that you wouldn't be able
to plot it. So what kind of array *is* Y? And what kind of array do you need
to provide to plot() in order to get multiple rows plotted at once? And how do
you get from what you have now to the kind of array you need?

Subject: Conversion to cell from double is not possible - Error

From: michael caro

Date: 5 May, 2010 00:42:44

Message: 11 of 11

On May 4, 7:03 pm, Walter Roberson <rober...@hushmail.com> wrote:
> michael caro wrote:
> > On May 4, 6:09 pm, Nathan <ngrec...@gmail.com> wrote:
> >> If so:
>
> >> for i = 1:na
> >>     Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
> >> end
> > Thank you for picking up on the mistake but thats not exactly what I'm
> > looking for.
> > How can I change the loop so matlab doesnt try to convert it into a
> > double?
>
> Nathan already answered that. Go back and reexamine the line, and "note the
> braces". His solution already prevents matlab from trying to convert into a
> double.
>
> > i'd like to create an array with 5 rows and 101 columns. the 5 rows
> > correspond to each different 'a' variable being evaluated with all of
> > the 't' variables, so i can then create a plot saying plot(t,Y)
>
> If you want to be able to do it with a single plot() call like you show, then
> you *do* want the values converted to double -- plot can handle a numeric Y
> array with multiple rows, but it cannot handle cell arrays to represent the rows.

Walter, thank you as well.

Tags for this Thread

No tags are associated with 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