Matlab coding and workspace

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?

2 个评论

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.

请先登录,再进行评论。

回答(1 个)

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

5 个评论

When I am running this code snippet for i=1:inf, then the workspace is not populating with variable and value stored in it on each iteration and waiting for loop to end so as to flash the variable in workspace.
First inf would be a very huge number. Show us your code and tell what you want.
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!!
##Code
% 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 = '127.0.0.1';
natnetclient.ClientIP = '127.0.0.1';
natnetclient.ConnectionType = 'Multicast';
natnetclient.connect;
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' )
return
end
% get the asset descriptions for the asset names
model = natnetclient.getModelDescription;
if ( model.RigidBodyCount < 1 )
return
end
% 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')
return
end
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];
assignin('base','var1',a)
fprintf( 'X:%0.1fmm ', data.RigidBody( i ).x * 1000 )
end
for i = 1:model.RigidBodyCount
fprintf( 'Name:"%s" ', model.RigidBody( i ).Name )
c=[c data.RigidBody( i ).y * 1000];
assignin('base','var2',c)
fprintf( 'Y:%0.1fmm ', data.RigidBody( i ).y * 1000 )
end
for i = 1:model.RigidBodyCount
fprintf( 'Name:"%s" ', model.RigidBody( i ).Name )
d=[d data.RigidBody( i ).z * 1000];
assignin('base','var3',d)
fprintf( 'Z:%0.1fmm\n', data.RigidBody( i ).z * 1000 )
end
end
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Performance and Memory 的更多信息

产品

版本

R2017b

标签

提问:

2019-6-13

评论:

2019-6-13

Community Treasure Hunt

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

Start Hunting!

Translated by