# State Machine to MATLAB code

Status
Not open for further replies.

#### chiu

##### Newbie level 3 how can i simulate the following state machine in MATLAB?
could you please write the code? im a newbie  Last edited:

#### andre_luis

##### Super Moderator
Staff member #### chiu

##### Newbie level 3 this is the block diagram my code is like this:

function [ y1,y2,D1,D2 ] = Encoder(input)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
length_input = length(input);
y1(1) = input(1);
D1(1) = 0;
D2(1) = 0;
y2(1) = input(1);
for i = 2:length_input
y1(i) = input(i);
y2(i) = xor(input(i),D2(i-1));
D1(i) = xor(y2(i-1),input(i));
D2(i) = xor(D1(i-1),y2(i-1));
end
end

i insert a group of date [1 1 0 1 1 0 0 0]

using the state machine ,the output should be [11 10 01 10 10 01 00 01]

but i run my code and got a set of wrong output which is [11 11 01 10 11 00 00 01]

could you please see if there is any problem in my code?

#### Siddharthsharma.mw

##### Newbie level 3 it might be easier to draw the state machine instead of writing it out textually. Stateflow (another product by MathWorks, makers of MATLAB and Simulink) is probably the best tool to represent state machine logic like this. Look at some of the getting started videos on the Stateflow website: