Using variable in a loop outside the loop
3 views (last 30 days)
Show older comments
I am getting real time data every 5seconds (with pause) with a loop and the data goes into matrix. How can I use the current data outside the loop and proceed to do computation with the most current data (the first row in the matrix)? When the loop is running I can not do anything until it is finished.. And I need to create infinite loop to continuously get the data.
1 Comment
Dhenish
on 8 Dec 2023
I have also this type of requirement so can you give me guidelines for this.
I also want variables value after every itration. i just want to change the value of varibales which is changing dynamiclly in every 5 seconds and need to store that variables in the base workspace.
Thanks in advance!!
Accepted Answer
Jan
on 15 May 2015
Matlab is single threaded. Creating an infinite loop to acquire data means that there is no "outside the loop". So better use a timer to read the data and let the main loop process the current values.
2 Comments
Walter Roberson
on 16 May 2015
The parallel computing toolbox would not be very effective for this, not in general. However you could create different labs, and in the lab that you were doing the reading of data, you could labSend() the new value to the other routine.
As the data will be received asynchronously, you would want to have that other code use labProbe to determine whether it has been sent anything, and if so to labReceive() it. Be sure to loop on that just in case multiple values have been received.
More Answers (1)
Walter Roberson
on 16 May 2015
Pretty much all of the data reception methods allow you to set a callback when data is ready (though if you are using the session-based interface on the DAQ and are waiting for a Trigger, then you might have to add a listener.) You would configure the data reception to trigger the callback every time the data was ready, and in the callback you would receive the data and store it away in a known location.
Meanwhile in your data processing loop, you would check once per loop to see if new data was received, and you would update your matrices if so.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!