Clear Filters
Clear Filters

How to fix error in Simulink "data read before being written to" ?

59 views (last 30 days)
I get this message when I try to run my simulation of a simple elevator system. How can I fix this?
  2 Comments
Arturo Garcia
Arturo Garcia on 25 Jun 2018
Edited: Arturo Garcia on 25 Jun 2018
Ideally this is what should be happening. "Input to the chart comes from a digital clock, and the Signal builder block corresponding to button pushes. These inputs are named “button” and “time”. Output is “position” and “velocity”. Four states are used to make sure that the elevator does not violate the limits of ground and top floor. When starting a session, the default state Stop is entered first, passing over to either Read or Read2. The output variable “velocity” gets the current value on the desired speed (button), which comes from a Signal Builder block. The ReadKeyboard state passes over to RunMotor, on condition that the velocity will not cause the position to be more than one thousand or less than zero. When entering RunMotor, the position is updated, calculating this using the simulated time, velocity and the previous position. Upon exiting the RunMotor state, the variable “old_position” is set in order to be used when calculating the position the next time. The passing between RunMotor and Read/Read2 continues as long as the position has not reached ground or top floor. The states Read and Read2 are different in that RunMotor passes over to Read if the desired speed is positive and to Read2 if the speed is negative. Whenever the position has reached zero or one thousand, the RunMotor state will pass over the execution to the state Stop. This will set the velocity to zero, and the chart will not reach RunMotor until the direction of the speed changes."

Sign in to comment.

Answers (2)

Fangjun Jiang
Fangjun Jiang on 25 Jun 2018
If you don't care that much, go to Model Configuration Parameters (Ctrl+E), Diagnostics, Stateflow, Read-before-write to output in Moore chart, change the setting.

Dalton L'Heureux
Dalton L'Heureux on 18 Nov 2022
I know this is an old post now, but figured I'd provide an answer incase anyone else runs into this. The statemachine captured in the image defualts into the Hold state. On the next time step, the first thing the statemachine is going to do is check its first priority transition from Hold to ReadKeyboard2. This condition is "[button < 0 && position > 0]." By just looking at the chart, I can't tell how the symbols "button" and "position" have been defined, but the error is telling us that at least the "position" symbol has not been initialized at the point at which Stateflow is trying to evaluate this condition. This means the value of "position" coud be anything... something stored in the memory location of a previous simulation perhaps... an untrustworthy value in otherwords. In the chart, the "position" symbol does not get explicitly defined until the statemachine enters the RunMotor state. The fix is to simply make sure all the symbols used in the statemachine are properly initialized (whether that be done internally within, or externally to the chart doesn't matter).

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!