Using variable in a loop outside the loop

2 次查看(过去 30 天)
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 个评论
Dhenish
Dhenish 2023-12-8
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!!

请先登录,再进行评论。

采纳的回答

Jan
Jan 2015-5-15
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 个评论
Fiction
Fiction 2015-5-15
编辑:Fiction 2015-5-17
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 2015-5-16
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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2015-5-16
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 个评论
Fiction
Fiction 2015-5-16
编辑:Fiction 2015-5-16
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).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by