Thread Subject: State Machine in Matlab

Subject: State Machine in Matlab

From: Philipp

Date: 8 Jun, 2006 06:07:15

Message: 1 of 7

Hi all

I have to control some hardware and I think for this the best
solution would be some kind of state machine. I have programmed state
machines in VHDL and C, so I wanted to know if I could do it in the
same way as in C or if there is a specific nice way to do that? So I
was thinking of having a timer that calls a function that contains
the single states. For me it sounds okay, but just wanna make sure if
there is anything i have consider.

Best Wishes
Philipp

Subject: State Machine in Matlab

From: Philipp

Date: 8 Jun, 2006 06:57:08

Message: 2 of 7

Oh and I dont have the stateflow Box for Matlab to implement a
Statemachine

Subject: State Machine in Matlab

From: Predictor

Date: 8 Jun, 2006 05:31:18

Message: 3 of 7


Philipp wrote:
> I have to control some hardware and I think for this the best
> solution would be some kind of state machine. I have programmed state
> machines in VHDL and C, so I wanted to know if I could do it in the
> same way as in C or if there is a specific nice way to do that? So I
> was thinking of having a timer that calls a function that contains
> the single states. For me it sounds okay, but just wanna make sure if
> there is anything i have consider.

I may be missing something, but wouldn't this require a loop and a
switch/case?

State = 1; while State ~= 99, disp(['State: ' int2str(State)]), switch
State, case 1, State = 2; case 2, State = 99;end, end


-Will Dwinnell
http://will.dwinnell.com

Subject: State Machine in Matlab

From: Philipp

Date: 8 Jun, 2006 10:27:58

Message: 4 of 7

> State = 1; while State ~= 99, disp(['State: ' int2str(State)]),
> switch
> State, case 1, State = 2; case 2, State = 99;end, end

Well i was thinking of a timer and a switch/case.

So every time I get a timer interrupt I call the function which
contains my singles states, and here I will work with the switch
statement. Do you think this is a proper way to do it or is it better
with the look?

Subject: State Machine in Matlab

From: Michael Salloker

Date: 8 Jun, 2006 17:05:04

Message: 5 of 7

Philipp schrieb:
>>State = 1; while State ~= 99, disp(['State: ' int2str(State)]),
>>switch
>>State, case 1, State = 2; case 2, State = 99;end, end
>
>
> Well i was thinking of a timer and a switch/case.
>
> So every time I get a timer interrupt I call the function which
> contains my singles states, and here I will work with the switch
> statement. Do you think this is a proper way to do it or is it better
> with the look?

Yes there is an alternative/better? way. For every state you have to do
a job. Put this jobs in functions. Create a function handle to these
functions. Create an array with the function handles so that the
position of the function handle in the array corresponds with the number
of the state, from which the function is called. So you need no
switch-case construction (or somewhat similar), you still have to call a
fuction which handle you get from the array using the state number for
indexing.

Michael

Subject: State Machine in Matlab

From: Philipp

Date: 9 Jun, 2006 02:26:05

Message: 6 of 7

> Yes there is an alternative/better? way. For every state you have
> to do
> a job. Put this jobs in functions. Create a function handle to
> these
> functions. Create an array with the function handles so that the
> position of the function handle in the array corresponds with the
> number
> of the state, from which the function is called. So you need no
> switch-case construction (or somewhat similar), you still have to
> call a
> fuction which handle you get from the array using the state number
> for
> indexing.

Interesting approach i have to admit. But is there some advantage
compared to my suggestion? Is the way you explained the standard way
to implement a state machine?

Cheers
Philipp

Subject: State Machine in Matlab

From: Michael Salloker

Date: 9 Jun, 2006 14:34:30

Message: 7 of 7

Philipp schrieb:
>>Yes there is an alternative/better? way. For every state you have
>>to do
>>a job. Put this jobs in functions. Create a function handle to
>>these
>>functions. Create an array with the function handles so that the
>>position of the function handle in the array corresponds with the
>>number
>>of the state, from which the function is called. So you need no
>>switch-case construction (or somewhat similar), you still have to
>>call a
>>fuction which handle you get from the array using the state number
>>for
>>indexing.
>
>
> Interesting approach i have to admit. But is there some advantage
> compared to my suggestion? Is the way you explained the standard way
> to implement a state machine?
>
> Cheers
> Philipp

I have not seen a standard way for implementing a state machine in a
textual computer language (there exist garphical solutions like
Stateflow) with exception of the IEC 61131 for PLCs.

This list based approach is in my opinion a simple solution were you get
rid of long switch-case constructions. You can also apply this solution
C (using function pointers instead of the handles). To me it's a short
and elegant solution.

But try out different solutions and find out which one is the best for
you and your problem.

Michael

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com