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:
how to access a value in a variable and send into a struct

Subject: how to access a value in a variable and send into a struct

From: sathish kumar

Date: 1 Jan, 2013 11:29:08

Message: 1 of 4

i want access the value of a variable 'start', iam getting error as undefined variable or function 'start'
i have not defined ''start'' before this part of code,directly i am assigning value of i to the ''start'' variable
 here is my code
for i=1:length(group)
    if fg >= g(i);
        if kg >= l(i);
            if max h == h(i);
                start = i
                break;
            end
        end
    end
end
do_game=cri{start};

here cri=sem_de;
         sem_de{j}=sem;


please tell me how to define a variable ''start'' in this code

Subject: how to access a value in a variable and send into a struct

From: Nasser M. Abbasi

Date: 1 Jan, 2013 11:42:38

Message: 2 of 4

On 1/1/2013 5:29 AM, sathish kumar wrote:
> i want access the value of a variable 'start', iam getting error as undefined variable or function 'start'
> i have not defined ''start'' before this part of code,directly i am assigning value of i to the ''start'' variable
> here is my code
> for i=1:length(group)
> if fg >= g(i);
> if kg >= l(i);
> if max h == h(i);
> start = i
> break;
> end
> end
> end
> end
> do_game=cri{start};
>
> here cri=sem_de;
> sem_de{j}=sem;
>
>
> please tell me how to define a variable ''start'' in this code
>

Have you thought what might happen if you finish the above loop
over length(group) and the statment `start=i` is never executed?

What would happen in this case when you get to the line below
this loop which says

          do_game=cri{start};

would not 'start' be undefined in this case?

Also, what exactly is this suppose to mean?

         if max h == h(i);

And why not replace all these `if` nesting with one logical && ?

--Nasser

Subject: how to access a value in a variable and send into a struct

From: sathish kumar

Date: 2 Jan, 2013 08:27:07

Message: 3 of 4

"Nasser M. Abbasi" wrote in message <kbui3c$17i$1@speranza.aioe.org>...
> On 1/1/2013 5:29 AM, sathish kumar wrote:
> > i want access the value of a variable 'start', iam getting error as undefined variable or function 'start'
> > i have not defined ''start'' before this part of code,directly i am assigning value of i to the ''start'' variable
> > here is my code
> > for i=1:length(group)
> > if fg >= g(i);
> > if kg >= l(i);
> > if max h == h(i);
> > start = i
> > break;
> > end
> > end
> > end
> > end
> > do_game=cri{start};
> >
> > here cri=sem_de;
> > sem_de{j}=sem;
> >
> >
> > please tell me how to define a variable ''start'' in this code
> >
>
> Have you thought what might happen if you finish the above loop
> over length(group) and the statment `start=i` is never executed?
>
> What would happen in this case when you get to the line below
> this loop which says
>
> do_game=cri{start};
>
> would not 'start' be undefined in this case?
>
> Also, what exactly is this suppose to mean?
>
> if max h == h(i);
>
> And why not replace all these `if` nesting with one logical && ?
>
> --Nasser



i have not understood u r idea
please tell me the way to define the variable ''start''
it is must to define variable or to change the index or to use logical operaters

Subject: how to access a value in a variable and send into a struct

From: Steven_Lord

Date: 2 Jan, 2013 15:02:47

Message: 4 of 4



"sathish kumar" <satty634@gmail.com> wrote in message
news:kc0r0r$8t5$1@newscl01ah.mathworks.com...
> "Nasser M. Abbasi" wrote in message <kbui3c$17i$1@speranza.aioe.org>...
>> On 1/1/2013 5:29 AM, sathish kumar wrote:
>> > i want access the value of a variable 'start', iam getting error as
>> > undefined variable or function 'start'
>> > i have not defined ''start'' before this part of code,directly i am
>> > assigning value of i to the ''start'' variable
>> > here is my code
>> > for i=1:length(group)
>> > if fg >= g(i);
>> > if kg >= l(i);
>> > if max h == h(i);
>> > start = i
>> > break;
>> > end
>> > end
>> > end
>> > end
>> > do_game=cri{start};
>> >
>> > here cri=sem_de;
>> > sem_de{j}=sem;
>> >
>> >
>> > please tell me how to define a variable ''start'' in this code
>> >
>>
>> Have you thought what might happen if you finish the above loop
>> over length(group) and the statment `start=i` is never executed?
>>
>> What would happen in this case when you get to the line below
>> this loop which says
>>
>> do_game=cri{start};
>>
>> would not 'start' be undefined in this case?
>>
>> Also, what exactly is this suppose to mean?
>>
>> if max h == h(i);
>>
>> And why not replace all these `if` nesting with one logical && ?
>>
>> --Nasser
>
>
>
> i have not understood u r idea
> please tell me the way to define the variable ''start''
> it is must to define variable or to change the index or to use logical
> operaters

Let's take a simpler example.

for k = 1:10
    if k == 345345
        z = 1;
    end
end
y = sin(z);

Now assuming z was not defined BEFORE that FOR loop starts, will the line of
code that defines it EVER be executed during that FOR loop's execution? Will
k EVER take on the value 345345? Since we're counting from 1 to 10 in
increments of 1, it better not! Therefore the variable z will NOT be defined
during the FOR loop's execution and when the "y = sin(z);" line executes
MATLAB will error.

To solve this, you need to determine what should happen if the conditions
that cause the line that defines start are never satisfied. To detect this,
put this line immediately before your FOR loop in your code:

start = NaN;

Immediately before the line "do_game=cri{start};" write:

if isnan(start)
    % HANDLE NONSTART CASE
end

and add code inside that IF statement.

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

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