Using variable in a loop outside the loop

4 views (last 30 days)
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
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!!

Sign in to comment.

Accepted Answer

Jan
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
Fiction
Fiction on 15 May 2015
Edited: Fiction on 17 May 2015
DO you know if parallel computing toolbox could do this? Thanks I will explore the timer function.
UPDATE: The timer objects seem to work fine.
Walter Roberson
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.

Sign in to comment.

More Answers (1)

Walter Roberson
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.
  1 Comment
Fiction
Fiction on 16 May 2015
Edited: Fiction on 16 May 2015
I am getting the data from the trading toolbox so the data acquisition is done trough one of the matlab functions. It creates a structure that is automatically updated on every 'tick' of the market. Every X seconds I get the current price(I do not care if new value or not I need the current price even if it did not change) from that structure and put it in a matrix, this is done with a loop. So far so good. But then I need the newest data from that loop to be sent to a model ( that could be even a running average).

Sign in to comment.

Categories

Find more on MATLAB 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!