Documentation

Change Data Values During Simulation

How to Change Values of Stateflow Data

When your chart is in debug mode, you can test the simulation by changing the values of data in the chart. After the debug>> prompt appears, as described in Watch Stateflow Data in the MATLAB Command Window, you can assign a different value to your data. To change a data value, enter the new value at the prompt using the following format:

data_name = new_value

For a list of data that you cannot change, see Data That Is Read-Only During Simulation. You cannot change message data values at the command line.

Examples of Changing Data Values

Scalar Example

Suppose that, after the debug>> prompt appears, you enter whos at the prompt and see the following data:

  Name         Size      Bytes  Class       
                                            
  airflow      1x1           1  uint8 array 
  temp         1x1           8  double array
To change...To this value...Enter...
airflow2airflow = uint8(2)
temp68.75temp = 68.75

If you try to enter airflow = 2, you get an error message because MATLAB® interprets that expression as the assignment of a double value to data of uint8 type. For reference, see Cases When Casting Is Necessary.

Multidimensional Example

Suppose that, after the debug>> prompt appears, you enter whos at the prompt and see the following data:

  Name                  Size                Bytes  Class       
                                                               
  ball_interaction      16x16                 256  int8 array 
  last_vel              16x2                  256  double array
  stopped               16x1                   16  int16 array 
To change...To this value...Enter...
The element in row 8, column 8 of ball_interaction1ball_interaction(8,8) = int8(1)
The element in row 16, column 1 of last_vel120.52last_vel(16,1) = 120.52
The last element in stopped0stopped(16) = int16(0)

One-based indexing applies when you change values of Stateflow® data while the chart is in debug mode.

Variable-Size Example

Suppose that, after the debug>> prompt appears, you enter whos at the prompt and see the following data:

  Name   Size   Bytes  Class                                
                                                               
  y1     1x1        8  double array (variable sized: MAX 16x16)
  y2     1x1        8  double array (variable sized: MAX 16x4)
To change...To...Enter...
y1A 10-by-5 array of onesy1 = ones(10,5)
y2A 6-by-4 array of zerosy2 = zeros(6,4)

Changing the dimensions of variable-size data works only when the new size does not exceed the dimension bounds.

Fixed-Point Example

Suppose that, after the debug>> prompt appears, you enter whos at the prompt and see the following data:

  Name      Size     Bytes  Class                         
                                                          
  y_n1      1x1          2  fixpt (int16 array (2^-10)*SI)
  x_n1      1x1          2  fixpt (int16 array (2^-12)*SI)

Both y_n1 and x_n1 have signed fixed-point types, with a word length of 16. y_n1 has a fraction length of 10 and x_n1 has a fraction length of 12.

To change...To this fixed-point value...Enter...
y_n10.5410y_n1 = fi(0.5412,1,16,10)
x_n10.4143x_n1 = fi(0.4142,1,16,12)

For more information about using fi objects, see the Fixed-Point Designer™ documentation.

Enumerated Example

Suppose that, after the debug>> prompt appears, you enter whos at the prompt and see the following data:

Name                  Size     Bytes  Class      
                                                 
CurrentRadioMode      1x1           4  int32 array
MechCmd               1x1           4  int32 array

Assume that CurrentRadioMode and MechCmd use the enumerated types RadioRequestMode and CdRequestMode, respectively.

To change...To this enumerated value...Enter...
CurrentRadioModeCDCurrentRadioMode = RadioRequestMode.CD
MechCmdPLAYMechCmd = CdRequestMode.PLAY

You must include the enumerated type explicitly in the assignment. Otherwise, an error appears at the debug>> prompt.

Limitations on Changing Data Values

Data That Is Read-Only During Simulation

You cannot change data of the following scopes while the chart is in debug mode:

  • Constant

  • Input

Limitations on Changing Type and Size

The following data properties cannot change:

  • Data type

  • Size

However, for variable-size data, you can change the dimensions of the data as long as the size falls within the dimension bounds. For example, varsizedData = ones(5,7); is a valid assignment for a variable-size 10-by-10 array.

Limitations for Fixed-Point Data

  • Do not assign a value that falls outside the range of values that the fixed-point type can represent. Avoid selecting a value that causes overflow.

  • Sign, word length, fraction length, slope, and bias cannot change.

Limitations for Structures

  • You cannot change the data type or size of any fields.

  • Addition or deletion of fields does not work because the size of the structure cannot change.

Cases When Casting Is Necessary

When you change a data value, you must explicitly cast values for data of the following built-in types:

  • single

  • int8

  • uint8

  • int16

  • uint16

  • int32

  • uint32

For example, the following assignments are valid:

  • my_data1 = uint8(2)

  • my_data2 = single(5.3)

Casting is not necessary when you change the value of data that is of type double.

Was this topic helpful?