Taking continuous inputs in MATLAB - matlab

I want to take data from bluetooth into Matlab every 10 to 15 seconds. I figured out a way to transfer data to Matlab via bluetooth. The problem I am facing is that I want Matlab to execute a set of commands after a time interval to take input from bluetooth. How can I do this?

You could create a TIMER OBJECT, that you set to execute a function that collects and processes data every 10 to 15 seconds.

Related

Creating delay with a while loop in Matlab

As a student I am currently working on a Matlab Simulink project. I am quite new to using Matlab/Simulink (few weeks).
I want to implement and run a Matlab “.m” file with which I can open Simulink and start the simulation. The aim is to do a 24h Test with a load cell cut into 1h “pieces” and to save the data to different sheets of an excel file each hour. So my simulation runs for 1h, stops and starts again, and so on. Through Matlab and a “for” loop I do the measures 24 times.
Between measuring steps I have to wait for simulink to finish its measures and saving the file in order for the Simulink window to be able to get closed by close_system('Thesis_SerDatTransm_Simulink').
So I tried to implement the delay with a while loop and by checking if the measures I get fit into an array of the size bigger than 449 (I measure 449 values):
for k=0:1:24
% Load Simulink
load_system('Thesis_SerDatTransm_Simulink.slx')
% Open Simulink
open_system('Thesis_SerDatTransm_Simulink.slx')
% Start Simulation
set_param('Thesis_SerDatTransm_Simulink', 'SimulationCommand', 'Start');
% Save Data
my_cell = sprintf('A%d',k);
xlswrite('file.xlsx',y,my_cell)
% Wait for Simulation
while 1
test=size(y)>=449;
if (test)
close_system('Thesis_SerDatTransm_Simulink')
break
end
end
end
The Problem now is, that program gets stuck at the while loop. Simulink is started, but no simulation or data gathering is done.
So I wondered if anyone could check if something is wrong with my While loop, since the rest of the programm works all fine without the loop (but receiving an error message, that during the simulation, Simulink window can't be closed).
I know there is a way to create a delay with waitforin matlab and create another function which I could call, but I couldn't figure out how to do this yet.
thanks
Regards
hohmchri
The right way to do this is to use the sim function to run your model (not the sequence of load_system, open_system and set_param that you have).
sim will block the execution of m-code until the model completes executing. Data can either be returned into the workspace (when used with no output arguments) or returned as an output from the call to sim. (And then you can write it to Excel as you've done.)
The only reason not to use sim, and perhaps use the commands you have, is if the model takes a long time to initialize, and you don't want to to open and close it every time through the loop. However, even in this case your code isn't correct. The load_system would be outside the loop; the open_system is not required; in your while loop you would poll the model's SimulationStatus property to see if it is still running (not the size of the y variable); and the close_system would be after the loop (as indicated by #m_power in one of the comments).
As written you should use the matlab pause command. This stops your execution for X seconds.
You should also look to optimize your code as m_power states

Isolate Seconds from Matlab Clock

I am trying to achieve two things:
Start a function if the current time is an exact 10 seconds multiple and
Run the function in a loop every 10 seconds after that
I have a cronjob on AWS pulling data every 10 seconds. I want my MATLAB function to run specifically once that has been updated (every 10 seconds).
The function takes less than 10 seconds to run. Currently, I am doing:
tic
function
toc
elapsedTime=toc;
pause(10-elapsedTime);
but this dosent seem to be working. On top of that, I want the entire function to start at 1:00:10 (seconds).
I think using Matlab's
format shortg
c = clock
fix(c)
and some how starting at sec = [0,10,20....,60] would work, but I am not sure how to isolate the seconds from the system clock.

matlab script node in Labview with different timing

I have a DAQ for Temperature measurment. I take a continuous sample rate and after DAQ, calculating temperature difference per minute (Cooling Rate: CR) during this process. This CR and temperature values are inserted into the Matlab script for a physical model running (predicting the temperature drop for next 30 sec). Then, I record and compare the predicted and experimental values in LabVIEW.
What i am trying to do is the matlab model is executing every 30 sec, and send out its predictions as an output from matlab script. One of this outputs helps me to change the Air Blower Motor Speed until next matlab run( eventually affect the temperature drop for next 30 sec as well, which becomes a closed loop). After 30 sec while main process is still running, sending CR and temperature values to matlab model again, and so on.
I have a case structure for this Matlab script. And inside of case structure i applied an elapsed time function to control the timing for the matlab script, but this is not working.
Yes. Short answer: I believe (one of) the reasons the program behaves weird on changed timing are several race conditions present in the code.
The part of the diagram presented shows several big problems with the code:
Local variables lead to race conditions. Use dataflow. E.g. you are writing to Tinitial local variable, and reading from Tinitial local varaible in the chunk of code with no data dependencies. It is not known whether reading or writing will happen first. It may not manifest itself badly with small delays, while big delays may be an issue. Solution: rewrite you program using the following example:
From Bad:
To Good:
(nevermind broken wires)
Matlab script node executes in the main UI execution system. If it is executing for a long time, it may freeze indicators/controls as well as execution of other pieces of code. Change execution system of other VIs in your program (say to "other 1") and see if the situation improves.

MATLAB .....Get audio data from recordObj while recording function is in progress

The matlab recording function"record ( recordObj, samplingTime )" needs 0.8 second plus the sampling time to be executed.
this means that if i want to record for only 0.2 second the execution time of this function will be 1 second.
I am working on a real time processing project in which I need to record 0.2 second files with high frequency and make real time processing on each file.
So i tried to record a long record and access it every 0.2 second.
So I wonder if i can access recordObj while the recording function is in progress .
I tried this code but i got error as the matlab couldn't access "myvoise" while recording is in progress. thanks in advance
clc
% clear all
% myVoice = audiorecorder;
% % Define callbacks to show when
% % recording starts and completes.
% myVoice.StartFcn = 'disp(''Start speaking.'')';
% myVoice.StopFcn = 'disp(''End of recording.'')';
% record(myVoice,20);
% y=getaudiodata(myVoice);
Unfortunately, Matlab is not really designed for real time processing. But if you really need it, look into the DSP Systems Toolbox, which provide this functionality.
You couldn't access myVoice because record is a not blocking function, this means that after execution of record(myVoice,20) immediately getaudiodata is executed but myVoice has not yet captured any data. If instead you use recordblocking(myVoice,20) then this will block flow of your code for 20 seconds. So myVoice now will contain data for 20 seconds and getaudiodata will be successfully executed.
Because you want real time operation I suggest set myVoice.TimerFcn = 'callbackfcn(myVoice)' and also set myVoice.TimerPeriod=period. Where callback fcn will be a user specified function in which you call data = getaudiodata(myVoice). This function will be called every period seconds during execution. So in this way you can call record(myVoice,20) and after period(s) getaudiodata will be successfully executed because myVoice already has period(in s) of audio data.
Be aware of that every time getaudiodata is executed will acquire all data of my voice from the beggining of recording, so you can skip every period the previous acquired data (i*period/Fs) where i is the time the callback function is executed(beginning from zero). Be also awhere of that myVoice will be buffered in memory, so if you record for a long period of time at high sampling frequency matlab performance will deteriorate

Wait using s-function

I am using a s-function built in Simulink and I need to implement a waiting time. For example I need to do this :
send the first frame
wait 20 ms
send the second frame
wait 20 ms
send third frame
How could I establish this waiting time between 2 frames. I am using C language and a Level-1 Matlab S-function.
First of all, it is very difficult to get millisecond accuracy. it depends a lot on your hardware, OS, proccesses running..
you could try to achieve it by simply using the pause command
send the first frame
pause(0.020)
send the second frame
pause(0.020)
send third frame
or using timer objects http://www.mathworks.com/help/matlab/ref/timerclass.html
both solutions are not accurate. the best solution would be to base your timing in external events. is there any event triggered after sending each frame?