To read a value of a readable field (either
eventOut), first synchronize that field with the
vrnode/sync method. After synchronization, each time the field changes in the
scene, the field value updates on the host. You can then read the value of the field with the
vrnode/getfield method or directly access the field value using dot
The virtual scene for the Magnetic
Levitation Model example,
maglev.wrl, contains a PlaneSensor
'Grab_Sensor'). The PlaneSensor is
attached to the ball geometry to register your attempts to move the ball up or down when
grabbing it using the mouse. The example uses the sensor fields
maxPosition to restrict movement in
other directions. You can use the output of the sensor translation field as the new setpoint
for the ball position controller. You can read the sensor output value into a MATLAB® variable setpoint.
Create the vrworld object and open the world.
wh = vrworld('maglev.wrl'); open(wh);
Get the node handle.
nh = vrnode(wh, 'Grab_Sensor');
Synchronize the translation field.
sync(nh, 'translation', 'on');
Read the synchronized field value, using one of these three alternatives:
setpoint = getfield(nh, 'translation'); setpoint = nh.translation; setpoint = wh.Grab_Sensor.translation;
To use the setpoint value in a Simulink® model, you can write an S-function or a MATLAB Function block that reads the sensor output periodically. This example uses an S-function.
Right-click the VR Sensor Reader block of Magnetic Levitation Model
vrmaglev) model and select Mask > Look Under Mask.
vrmaglev/VR Sensor Reader model displays. This model
vrextin block, which is an S-function block. The
vrextin S-function synchronizes the sensor field in the
method and periodically reads its value in the
Examine the S-function parameters. Right-click
select S-Function Parameters.
The parameters defined in the mask supply the sample time, virtual world, and the node and field to read.
Instead of setting its own block outputs, the
S-function sets the value of the adjacent Constant block
value_holder. This setting makes the VR Sensor
Reader block compatible with Simulink
Coder™ code generation so that the model can run on Simulink
The signal loop between user action (grabbing the ball to a desired position using
a mouse) closes through the associated Simulink model
vrmaglev. Grabbing the ball to a new position
works only when the model is running and when the model sets the blue selection method
switch to the virtual reality sensor signal path. To experience the behavior of the
PlaneSensor using the virtual scene only, save the
file under a new name. Remove the comment symbol (#) to enable the last line of this
file. These actions activate direct routing of sensor output to a ball translation.
Then you can experiment with the newly created scene instead of the original
ROUTE Grab_Sensor.translation_changed TO Ball.translation
You can use this approach to input information from all node fields of the type
eventOut, not only a Sensor
eventOut field. See VRML Data Class Types for more information about
virtual world data class types.
For fields of class
exposedField, you can use an alternate name
using the field name with the suffix,
_changed. For example,
alternate names for requesting the translation field value of the