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:
For Loop Problem

Subject: For Loop Problem

From: Pete

Date: 13 Feb, 2013 13:45:07

Message: 1 of 6

Hi,
I am trying to use a For Loop to assign each individual number in a 31x1 matrix a letter. I have tried to create a For loop but i keep getting an error saying that it cannot label the point as there are not enough points in the matrix, i.e there are 31 numbers in the matrix and it is trying to find the 32nd. What am i doing wrong? Here is what i have at the moment:

for j=1:length(Tr)
    A=Tr(j);
    B=Tr(j+1);
end

Thanks

Subject: For Loop Problem

From: Jos (10584)

Date: 13 Feb, 2013 14:22:06

Message: 2 of 6

"Pete " <harri.short@hotmail.com> wrote in message <kfg5d3$6k6$1@newscl01ah.mathworks.com>...
> Hi,
> I am trying to use a For Loop to assign each individual number in a 31x1 matrix a letter. I have tried to create a For loop but i keep getting an error saying that it cannot label the point as there are not enough points in the matrix, i.e there are 31 numbers in the matrix and it is trying to find the 32nd. What am i doing wrong? Here is what i have at the moment:
>
> for j=1:length(Tr)
> A=Tr(j);
> B=Tr(j+1);
> end
>
> Thanks

In the line B=Tr(j+1) you want to retrieve the "j+1"-th element of Tr. If j reached the value of length(Tr), Tr(j) is the last element, but there is no Tr(j+1) element anymore, of course.
This is causing the error.

~ Jos

Subject: For Loop Problem

From: Steven_Lord

Date: 13 Feb, 2013 14:22:28

Message: 3 of 6



"Pete " <harri.short@hotmail.com> wrote in message
news:kfg5d3$6k6$1@newscl01ah.mathworks.com...
> Hi,
> I am trying to use a For Loop to assign each individual number in a 31x1
> matrix a letter. I have tried to create a For loop but i keep getting an
> error saying that it cannot label the point as there are not enough points
> in the matrix, i.e there are 31 numbers in the matrix and it is trying to
> find the 32nd. What am i doing wrong? Here is what i have at the moment:
>
> for j=1:length(Tr)
> A=Tr(j);
> B=Tr(j+1);

When j is equal to length(Tr), Tr(j+1) is one past the end of the array. How
to fix this depends on what exactly you're trying to do. There are likely
ways to do what you described without a loop, depending on the specific
details behind your individual numbers. Are they integer values? Are they
unique? What criteria are you using to generate the number -> letter
mapping? If you post more information to the newsgroup, people may be able
to offer more specific suggestions.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: For Loop Problem

From: Pete

Date: 13 Feb, 2013 15:05:06

Message: 4 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <kfg7j3$enm$1@newscl01ah.mathworks.com>...
>
>
> "Pete " <harri.short@hotmail.com> wrote in message
> news:kfg5d3$6k6$1@newscl01ah.mathworks.com...
> > Hi,
> > I am trying to use a For Loop to assign each individual number in a 31x1
> > matrix a letter. I have tried to create a For loop but i keep getting an
> > error saying that it cannot label the point as there are not enough points
> > in the matrix, i.e there are 31 numbers in the matrix and it is trying to
> > find the 32nd. What am i doing wrong? Here is what i have at the moment:
> >
> > for j=1:length(Tr)
> > A=Tr(j);
> > B=Tr(j+1);
>
> When j is equal to length(Tr), Tr(j+1) is one past the end of the array. How
> to fix this depends on what exactly you're trying to do. There are likely
> ways to do what you described without a loop, depending on the specific
> details behind your individual numbers. Are they integer values? Are they
> unique? What criteria are you using to generate the number -> letter
> mapping? If you post more information to the newsgroup, people may be able
> to offer more specific suggestions.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

No they are not integer values.
Tr=linspace(Cm1,Cm2,31)';
(Cm1 and Cm2 being values calculated previously) So Tr is a 31x1 matrix of values. I want to assign each value in the matrix an individual letter. So the first value in the matrix is A, the second value is B, third C and so on so all 31 values are assigned a letter. I think a For loop is the best way to do this as the values in Tr may change when i run different data in the script?

Subject: For Loop Problem

From: anyone

Date: 14 Feb, 2013 01:55:34

Message: 5 of 6

On Wed, 13 Feb 2013 15:05:06 +0000, Pete wrote:

> "Steven_Lord" <slord@mathworks.com> wrote in message
> <kfg7j3$enm$1@newscl01ah.mathworks.com>...
>>
>>
>> "Pete " <harri.short@hotmail.com> wrote in message
>> news:kfg5d3$6k6$1@newscl01ah.mathworks.com...
>> > Hi,
>> > I am trying to use a For Loop to assign each individual number in a
>> > 31x1 matrix a letter. I have tried to create a For loop but i keep
>> > getting an error saying that it cannot label the point as there are
>> > not enough points in the matrix, i.e there are 31 numbers in the
>> > matrix and it is trying to find the 32nd. What am i doing wrong?
>> > Here is what i have at the moment:
>> >
>> > for j=1:length(Tr)
>> > A=Tr(j); B=Tr(j+1);
>>
>> When j is equal to length(Tr), Tr(j+1) is one past the end of the
>> array. How to fix this depends on what exactly you're trying to do.
>> There are likely ways to do what you described without a loop,
>> depending on the specific details behind your individual numbers. Are
>> they integer values? Are they unique? What criteria are you using to
>> generate the number -> letter mapping? If you post more information to
>> the newsgroup, people may be able to offer more specific suggestions.
>>
>> --
>> Steve Lord slord@mathworks.com To contact Technical Support use the
>> Contact Us link on http://www.mathworks.com
>
> No they are not integer values.
> Tr=linspace(Cm1,Cm2,31)';
> (Cm1 and Cm2 being values calculated previously) So Tr is a 31x1 matrix
> of values.

...precisely. Now, length(Tr) is 31. What do you suppose will happen when the loop attempts to assign the value of
Tr(31 + 1) (<---big hint) to the variable "B"?

[...]

Subject: For Loop Problem

From: TideMan

Date: 14 Feb, 2013 03:09:07

Message: 6 of 6

On Thursday, February 14, 2013 4:05:06 AM UTC+13, Pete wrote:
> "Steven_Lord" <slord@mathworks.com> wrote in message <kfg7j3$enm$1@newscl01ah.mathworks.com>...
>
> >
>
> >
>
> > "Pete " <harri.short@hotmail.com> wrote in message
>
> > news:kfg5d3$6k6$1@newscl01ah.mathworks.com...
>
> > > Hi,
>
> > > I am trying to use a For Loop to assign each individual number in a 31x1
>
> > > matrix a letter. I have tried to create a For loop but i keep getting an
>
> > > error saying that it cannot label the point as there are not enough points
>
> > > in the matrix, i.e there are 31 numbers in the matrix and it is trying to
>
> > > find the 32nd. What am i doing wrong? Here is what i have at the moment:
>
> > >
>
> > > for j=1:length(Tr)
>
> > > A=Tr(j);
>
> > > B=Tr(j+1);
>
> >
>
> > When j is equal to length(Tr), Tr(j+1) is one past the end of the array. How
>
> > to fix this depends on what exactly you're trying to do. There are likely
>
> > ways to do what you described without a loop, depending on the specific
>
> > details behind your individual numbers. Are they integer values? Are they
>
> > unique? What criteria are you using to generate the number -> letter
>
> > mapping? If you post more information to the newsgroup, people may be able
>
> > to offer more specific suggestions.
>
> >
>
> > --
>
> > Steve Lord
>
> > slord@mathworks.com
>
> > To contact Technical Support use the Contact Us link on
>
> > http://www.mathworks.com
>
>
>
> No they are not integer values.
>
> Tr=linspace(Cm1,Cm2,31)';
>
> (Cm1 and Cm2 being values calculated previously) So Tr is a 31x1 matrix of values. I want to assign each value in the matrix an individual letter. So the first value in the matrix is A, the second value is B, third C and so on so all 31 values are assigned a letter. I think a For loop is the best way to do this as the values in Tr may change when i run different data in the script?

The question is: why are you assigning them a letter?

If it is to then use them as text on a plot or the column names in Excel, then one way to do that is to simply map char(65:90) to your Tr.
Type:
char(65:90)
to see what I mean.
char(65) is A, char(66) is B, etc

But if you plan to use A, B, etc as variable names, then this is very naughty and you SHOULD NOT do it.
Instead, you should use an array or a structure.
If this is what you plan to do, give us more details and we can give you a better strategy.

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