MATLAB Answers


Matlab coding and workspace

Asked by Nitin Sharma on 13 Jun 2019
Latest activity Commented on by Nitin Sharma on 13 Jun 2019
How do I update the value stored in variable in workspace in every iteration while an infinite FOR loop is running in my matlab script?


Why is the for loop infinite? Why not use a while loop nor a condition (break) statement to suffice the loop?
Even if I use the while loop, how am I going to get my objective of flashing variable along with value in workspace on every iteration? Because once break command is executed, I can’t go back to the loop again infinite times#live data streaming from camera.

Sign in to comment.





1 Answer

Answer by KSSV
on 13 Jun 2019

A = zeros(100,1) ; % initilaize
for i = 1:100
A(i) = rand ; % update the value in loop


Please find my matlab code below. Here I am trying to get position coordinates of an object in every iteration from camera software. The problem with this code is that I am not able to flash the x,y,z i.e. position coordinates on every iteration rather publishing it once the loop terminates. Since it is live data streaming, I cannot restrict the iteation or loop count here.
Thanks for your time!!
% function NatNetPollingSample
fprintf( 'NatNet Polling Sample Start\n' )
% create an instance of the natnet client class
fprintf( 'Creating natnet class object\n' )
natnetclient = natnet;
% connect the client to the server (multicast over local loopback) -
% modify for your network
fprintf( 'Connecting to the server\n' )
natnetclient.HostIP = '';
natnetclient.ClientIP = '';
natnetclient.ConnectionType = 'Multicast';
if ( natnetclient.IsConnected == 0 )
fprintf( 'Client failed to connect\n' )
fprintf( '\tMake sure the host is connected to the network\n' )
fprintf( '\tand that the host and client IP addresses are correct\n\n' )
% get the asset descriptions for the asset names
model = natnetclient.getModelDescription;
if ( model.RigidBodyCount < 1 )
% Poll for the rigid body data a regular intervals (~1 sec) for 10 sec.
fprintf( '\nPrinting rigid body frame data approximately every second for 10 seconds...\n\n' )
a = [];
c = [];
d = [];
for idx = 1 : 30
java.lang.Thread.sleep( 996 );
data = natnetclient.getFrame; % method to get current frame
if (isempty(data.RigidBody(1)))
fprintf( '\tPacket is empty/stale\n' )
fprintf( '\tMake sure the server is in Live mode or playing in playback\n\n')
fprintf( 'Frame:%6d ' , data.Frame )
fprintf( 'Time:%0.2f\n' , data.Timestamp )
for i = 1:model.RigidBodyCount
fprintf( 'Name:"%s" ', model.RigidBody( i ).Name )
a=[a data.RigidBody( i ).x * 1000];
fprintf( 'X:%0.1fmm ', data.RigidBody( i ).x * 1000 )
for i = 1:model.RigidBodyCount
fprintf( 'Name:"%s" ', model.RigidBody( i ).Name )
c=[c data.RigidBody( i ).y * 1000];
fprintf( 'Y:%0.1fmm ', data.RigidBody( i ).y * 1000 )
for i = 1:model.RigidBodyCount
fprintf( 'Name:"%s" ', model.RigidBody( i ).Name )
d=[d data.RigidBody( i ).z * 1000];
fprintf( 'Z:%0.1fmm\n', data.RigidBody( i ).z * 1000 )
disp('NatNet Polling Sample End' )
I do not understand what you mean by "flash" in this situation ?
I am sorry about that. By flash I mean to publish the variables in workspace.

Sign in to comment.