The program below contains a timer object definition followed by its start command. Then the program continues executing other statements.
My question is whether TimerFcn will be called exactly after 0.01 sec, or will it wait until the for-loop completes for the timer callback function to fire?
% My timer object
t = timer('TimerFcn',#(x,y)G2(z), 'StartDelay',0.01);
start(t);
% Other program statements
for i=1:m
...
end
Bottom line is that MATLAB is effectively single-threaded. So if a long operation is currently executing, the timer callback will not get a chance to run, and according to the timer object properties (read about BusyMode), will instead add the event to a queue which MATLAB will eventually go through when it first gets a chance..
From what I understand (this is my own speculation), MATLAB timers can interrupt execution in between statements, but NOT during a lengthy one.
So in theory it should run after 0.01 second, but there are no guarantees...
The documentation says the following:
Note: The specified execution time and the actual execution of a timer
can vary because timer objects work in the MATLAB single-threaded
execution environment. The length of this time lag is dependent on
what other processing MATLAB is performing. To force the execution of
the callback functions in the event queue, include a call to the
drawnow function in your code. The drawnow function flushes the event
queue.
There is also this note on another doc page:
Note: Callback function execution might be delayed if the callback involves a CPU-intensive task such as updating a figure.
Related
Suppose I am running a function fun(), but I don't have access to inside it (so I can't put conditions inside it).
The function might be slow, for some inputs. How can I terminate the program if it takes more than a certain amount of time?
Update: I am testing the function for various set of inputs. For some of them it takes more. I want to skip the ones which take too long, and move to the next input.
Unfortunately MATLAB's single-threaded nature makes this more complicated than it should be. My first inkling is to use a timer, but even timer callbacks will not interrupt a busy MATLAB, as all M-Code is executed from the same thread.
I would solve this problem by calling the function from another MATLAB process, and monitoring that process. You can use the built-in SYSTEM function to call MATLAB, and use the -r command line argument to specify the name of the script to run. The pseudocode would look something like this. This rough and untested but should give you the idea:
% Create Timer object
timerObj = timer();
% set timer properties, with 60 second interval
set(timerObj, 'executionMode', 'singleShot', 'StartDelay', 60, 'timerFcn', #timerCallback);
% call MATLAB. It will run in background
system( 'matlab.exe -r myscript &' );
function timerCallback(varargin)
% if the other matlab process is still running when the timer is elapsed, kill it. perhaps use another system() call to run taskkkill.exe (if on windows)
I am using MATLAB GUI where a callback function calls so many other complex functions. If that callback function is called once than after its execution, all further processing get delayed. If that callback function is never called than processing speed is normal. What can be the issue?
If it's memory issue, I want to clear all memory and pending tasks for complex callback function after its execution so that the further processing is not delayed. Any help?
I tried clear function and clear variables at the end of a complex callback function but no improvement.
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
So basically what I want to do is have my main program to do some calculations, unrelated to a parallel program. The parallel program is constantly checking for some event to come true, and when it does, I want the main program to freeze and start another parallel job. Is it possible to do so in Matlab?
You can imagine it as a robot riding (main program) and at the same time checking its sensor data (worker). When it approaches an obstacle, a program to avoid the obstacle is started.
Thanks in advance,
Rugile :)
The best solution to this that I have seen is to use a Matlab timer object.
Implementation code would look something like this:
%Setup timer
t = timer;
t.ExecutionMode = 'fixedSpacing'; %See `docsearch Timer Object Execution Modes` for explaination
t.Period = 1; %Number of seconds after one execution to the start of the next
t.TimerFcn = #checkAndExecuteParallelJob
start(t);
%Start main job
mainJob();
In another file
function checkAndExecuteParallelJob
if (conditionIsFalse)
%Fast return
return;
end
%..Code to execute parallel job. This will block the main execution
Matlab is not multi-threaded, so the execution of a timer callback function will interrupt and block the execution of any main function. Timers can also interrupt each other, sometimes but not always, with a complex set of rules that I once tried to reverse engineer but have since given up on. However, for the relatively simple problem you laid out, I think that a timer object would be sufficient.
I'm using Psychtoolbox in MATLAB to run a behavioral psychology paradigm. As part of the paradigm, users have to view a visual stimulus and respond to it using some input mechanism. For a keyboard, this works as follows:
show stimulus
poll keyboard for response
if no response detected, loop back to 1
if response detected, break and move on with script
This works fine for a keyboard, as step 2 takes between 1-2 ms. The problem comes when I use an alternate input mechanism; in that case, step 2 takes ~20 ms. (I need this alternate input to run the study, and that should be considered immutable fact.) As the stimulus changes with a very short timespan, this added delay breaks the task.
My current thought is to try to use the parallel processing, such that one thread shows the stimulus, and another thread polls the keyboard. I'm currently using the Parallel Computing Toolbox to do this. The problem I'm having is that I don't know how to direct keyboard input to a "parallelized" thread. Does anyone know (1) whether it's possible to direct keyboard input to a thread / have a thread send a visual signal to a monitor, and if yes, (2) how to do it?
Also, if anyone has any better ideas as to how to approach this problem, I'm all ears.
According to this MATLAB newsgroup thread, it appears that threads can't modify graphics objects. Only the desktop MATLAB client can do that. This means that you can't handle updating of graphics from a thread, and I can confirm this as I tried it and wasn't able to modify figures or even the root object from a thread.
However, I think you may be able to do the main graphics updating in MATLAB while a thread handles polling for your input. Here's a sample function for continuously updating a display until a thread waiting for input from KbCheck is finished running:
function varargout = plot_until_input
obj = createJob(); %# Create a job
task = createTask(obj,#get_input,4,{deviceNumber}); %# Create a task
submit(obj); %# Submit the job
waitForState(task,'running'); %# Wait for the task to start running
%# Initialize your stimulus display here
while ~strcmp(get(task,'State'),'finished') %# Loop while the task is running
%# Update your stimulus display here
end
varargout = get(task,'OutputArguments'); %# Get the outputs from the task
destroy(obj); %# Remove the job from memory
%#---Nested functions below---
function [keyIsDown,secs,keyCode,deltaSecs] = get_input(deviceNumber)
keyIsDown = false;
while ~keyIsDown %# Keep looping until a key is pressed
[keyIsDown,secs,keyCode,deltaSecs] = KbCheck(deviceNumber);
end
end
end
I was able to successfully run the above function with some simple plotting routines and replacing the code in get_input with a simple pause statement and a return value. I'm unsure whether KbCheck will work in a thread, but hopefully you will be able to adapt this for your needs.
Here's the documentation for the Parallel Computing Toolbox functions used in the above code: createJob, createTask, submit, waitForState, destroy.
I don't know of a way how you could do this with parallel processing.
However, a feature you might be able to use is the timer object. You would set up the timer object to poll the input mechanism, and, if anything is detected, change the value of a global variable. Then, you start your stimulus routine. In the while-loop in which you're updating the display, you keep checking the global variable for a change from the timer object.
You have to tackle the 20ms latency in your input device. If it's too slow then get another input device. You can get good sub-millisecond timing with proper response boxes.
All this talk about threading is misguided and not applicable to the PTB framework.