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

madhan ravi
madhan ravi 2019년 6월 13일
Why is the for loop infinite? Why not use a while loop nor a condition (break) statement to suffice the loop?
Nits
Nits 2019년 6월 13일
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개)

KSSV
KSSV 2019년 6월 13일

0 개 추천

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

댓글 수: 5

Nits
Nits 2019년 6월 13일
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.
KSSV
KSSV 2019년 6월 13일
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' )
Walter Roberson
Walter Roberson 2019년 6월 13일
I do not understand what you mean by "flash" in this situation ?
Nits
Nits 2019년 6월 13일
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