I am currently trying to display data that I receive via serial port in a Matlab app designer application. I am suffering from abysmal refresh rate of the linear gauges (~1 Hz).
The gauges' values are updated by a fixed-rate timer which is set to 30Hz.
A timestamp print in the timer callback shows me that it is called with the correct frequency. My computer is quite beefy and the Task Manager doesn't show any hints of high load - in fact, the MATLAB app consumes almost no CPU time at all. It's actually not just the gauges but all UI elements.
So my guess - or better: my hope - is that there must be some hard cap on the refresh rate. The official documentation doesn't give any hints about how to change this, though.
My MATLAB version is R2016b.
So my question(s):
Is this fixed with R2017a?
If not: can I do something about it, i.e. fiddling with MATLAB builtin files?
Here's a MCV example demonstrating the issue:
classdef testapp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
andwatchhowthisstartstospinsmoothlyGaugeLabel matlab.ui.control.Label
andwatchhowthisstartstospinsmoothlyGauge matlab.ui.control.SemicircularGauge
KeepturninghereKnobLabel matlab.ui.control.Label
KeepturninghereKnob matlab.ui.control.Knob
end
properties (Access = private)
tim_the_timer
i = 0;
end
methods (Access = private)
function app = refreshMeter(app, ~,~)
% display timestamp
datestr(now,'HH:MM:SS.FFF')
% update the gauge
app.andwatchhowthisstartstospinsmoothlyGauge.Value = app.i;
app.i = app.i + 1;
if app.i > 100
app.i = 0;
end
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
t = timer;
t.TimerFcn = #app.refreshMeter;
t.Period = 0.02;
t.BusyMode = 'drop';
t.ExecutionMode = 'fixedRate';
start(t);
app.tim_the_timer = t;
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
stop(app.tim_the_timer);
delete(app.tim_the_timer);
delete(app);
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, #UIFigureCloseRequest, true);
setAutoResize(app, app.UIFigure, true)
% Create andwatchhowthisstartstospinsmoothlyGaugeLabel
app.andwatchhowthisstartstospinsmoothlyGaugeLabel = uilabel(app.UIFigure);
app.andwatchhowthisstartstospinsmoothlyGaugeLabel.HorizontalAlignment = 'center';
app.andwatchhowthisstartstospinsmoothlyGaugeLabel.Position = [275 138 246 15];
app.andwatchhowthisstartstospinsmoothlyGaugeLabel.Text = '... and watch how this starts to spin smoothly';
% Create andwatchhowthisstartstospinsmoothlyGauge
app.andwatchhowthisstartstospinsmoothlyGauge = uigauge(app.UIFigure, 'semicircular');
app.andwatchhowthisstartstospinsmoothlyGauge.Position = [338 168 120 65];
% Create KeepturninghereKnobLabel
app.KeepturninghereKnobLabel = uilabel(app.UIFigure);
app.KeepturninghereKnobLabel.HorizontalAlignment = 'center';
app.KeepturninghereKnobLabel.Position = [119 265 112 15];
app.KeepturninghereKnobLabel.Text = 'Keep turning here...';
% Create KeepturninghereKnob
app.KeepturninghereKnob = uiknob(app.UIFigure, 'continuous');
app.KeepturninghereKnob.Position = [145 314 60 60];
end
end
methods (Access = public)
% Construct app
function app = testapp()
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, #startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Edit: I noticed that the gauge gets immediately updated when I turn a knob, slider, etc. So somehow the capability of a higher refresh rate is definitely there... but how to enable it without having to touch controls? Updated MCV accordingly.
You should be able to force a refresh by calling the drawnow function in the refreshMeter callback.
I would suggest the following things to help with your problem:
follow this very useful link which provides mathworks solution for low-level graphics issues. It may have to do with your graphics card.
following #MrAzzamans suggestion, drawnow and refreshdata could help solve your problem (keep in mind they have different summoning options).
If you're using GUIDE, try changing to App Designer and see if it solves your problem.
Iv'e encountered similar problems when building a "video-player" in GUIDE, the first suggestion fixed it for me.
So i got in touch with MathWorks support regarding this issue. Unfortunately, this seems to be a known issue prevalent in both R2016b and 2017a that might be fixed in future releases. Support couldn't provide me with a workaround either, so for the time being i just print the measurement data to the matlab command line.
I found a way to force events triggering an update for a widget, for which drawnow was inoperate: try a sendkeys with a carriage return:
h = actxserver('WScript.shell');
h.SendKeys(newline);
Related
I want to code an easy audio filter app using the App Designer in Matlab. One should be able to load an audio file, press play and change parameters like input gain, cutoff frequency etc. while the file is being played.
I just cannot wrap my head around how to make it possible to change the parameters in realtime and update the corresponding variables so that one can hear how the filter is changing.
This is the code I have written by now:
classdef EulerFilter < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
CutoffKnobLabel matlab.ui.control.Label
CutoffKnob matlab.ui.control.Knob
PlayButton matlab.ui.control.StateButton
end
properties (Access = public)
inputGain % input Gain
CutoffHz % cutoff frequency in Hz
end
methods (Access = public)
function play(app)
% setup file stream
frameLength = 256;
fileReader = dsp.AudioFileReader(...
'Sun Behind CloudsDry.wav',...
'SamplesPerFrame',frameLength);
deviceWriter = audioDeviceWriter(...
'SampleRate',fileReader.SampleRate);
% code snippet
% porcessing of frames
while ~isDone(fileReader)
% code snippet
end
release(fileReader);
release(deviceWriter);
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.inputGain = 1;
app.CutoffHz = 22000;
end
% Value changed function: PlayButton
function PlayButtonValueChanged(app, event)
value = app.PlayButton.Value;
play(app);
end
% Value changing function: CutoffKnob
function CutoffKnobValueChanging(app, event)
%display(event)
changingValue = event.Value;
app.CutoffHz = changingValue;
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create CutoffKnobLabel
app.CutoffKnobLabel = uilabel(app.UIFigure);
app.CutoffKnobLabel.HorizontalAlignment = 'center';
app.CutoffKnobLabel.Position = [159 322 37 22];
app.CutoffKnobLabel.Text = 'Cutoff';
% Create CutoffKnob
app.CutoffKnob = uiknob(app.UIFigure, 'continuous');
app.CutoffKnob.Limits = [10 22000];
app.CutoffKnob.MajorTicks = [10 1000 5000 22000];
app.CutoffKnob.ValueChangingFcn = createCallbackFcn(app, #CutoffKnobValueChanging, true);
app.CutoffKnob.Position = [155 367 45 45];
app.CutoffKnob.Value = 22000;
% Create PlayButton
app.PlayButton = uibutton(app.UIFigure, 'state');
app.PlayButton.ValueChangedFcn = createCallbackFcn(app, #PlayButtonValueChanged, true);
app.PlayButton.Text = 'Play';
app.PlayButton.Position = [60 40 100 22];
end
end
methods (Access = public)
% Construct app
function app = EulerFilter
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, #startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
It is mostly the functions Matlab has generated for the GUI. I have added some properties which hold the values for input gain, cutoff etc. as well as the play() function which performs the signal processing.
I can run the app, press the play button and hear the audio file being played, but when I change the cutoff frequency for example, nothing changes. I guess this is because I execute the play() function inside the callback function when the play button was pressed and thus the callback functions when the cutoff knob is turned cannot be executed before the other one has finished.
When I first change the parameters and then press play, everything is correct except that I cannot change the parameters while the file is playing.
I have tried the following without success:
calling the callback function inside the while loop in the play() function, but I don't know what argument I have to pass for event (Matlab always tells me that it doesn't know the command or arguments are missing) or if this is even useful
execute the play() function inside the runStartupFcn() but this function is executed before the GUI is shown which is useless of course
I cannot add functions elsewhere as far as I can tell
So now the question is: Can I make the app work in realtime?
I am looking forward to your answers!
I think that the solution to your problem is creating a second thread. In Your main thread you have access to your handles and variables and your second thread runs the sound. My idea would have been including the changes that you do in thread A to thread B. Unaffortunately you can just use multithreading with the MatLab Parallel Processing Toolbox. I hope there is another way though.
Cheers, Pablo
There seem to be a lot of solutions/examples for Matlab's guide but not yet for the app designer.
It looks like your best bet is to call a pause function in the while loop to give your program time to get the updated values. Experiment with different pause times to make sure your program has enough time to update. Matlab can pause the current executing thread by using :
pause(0.001) % 0.001 sec
Or do a direct call to Java for more accuracy
java.lang.Thread.sleep(duration) % duration in msec
I'm pretty sure this will give your program the time to access the variables and update. This way you could check every 10/20/50/1000 loop cycles and update the parameters as often as you'd like to minimize any audible artifacts.
% Init counter to see how many loops have passed
counter = 0;
% processing of frames
while ~isDone(fileReader)
% Do your playback process stuff
if(counter > 10) % Updates every 10 loops or so
pause(0.001);
counter = 0;
end
counter = counter + 1;
end
Note: Code not tested, please let me know
Otherwise maybe have a look at a callback approach solution.
This is why GUIs in Matlab aren't always such a good idea :-) I understand why you might be doing this for a learning purpose but otherwise I would maybe investigate more integration with Java into your Matlab GUI's to handle all of the threading (or even the GUI design with Java itself). To get started...
May be too late to the thread to be of help, but the problem you're running into is when CALLBACK1 is invoked, it calls your PLAY() function, which does not run to completion until your file reading WHILE loop is complete. In other words, CALLBACK1 never finishes running until your file reading is complete.
If you change the cutoff frequency while CALLBACK1 is still reading the file, it (I assume) is calling its own callback, I'll refer to as CALLBACK2. Since MATLAB is single threaded, CALLBACK2 can't run until CALLBACK1 finishes running. Hence why you run into the issue you have.
The way to handle this is for CALLBACK1 to start a MATLAB TIMER object, and configure the timer object (which runs in a separate thread) to read the file at some frequency. This way, CALLBACK1 finishes running pretty quickly, while the timer object does your playing. This allows CALLBACK2 to execute and do its thing.
The complication you may still run into is whether you can change the cutoff frequency "on the fly" for the "playing" to reflect it. That's more a question whether the AudioFileReader object allows that.
Hope this helps.
I have a MATLAB GUI that calls an external function to make a plot (make_ethogram_plot).
The idea would be to have an external figure that is constantly updated with the output value from the figure. Every time the data gets updated it should replot the values, it updates at ~10 Hz. I chose gramm (https://github.com/piermorel/gramm/tree/master/%40gramm) because it is really easy to make a raster plot.
This is the function that gets called. I am having issues to
1) Make it only update in the parent figure with specific name, instead of plotting in the GUI(which is the active figure).
2) Make it not crash. It would open many figures or open or close the same figure at 10 Hz until crashing.
In this configuration, it gives error because it doesn't find g after the first plot. Making g , f, and p1 globals makes it crash (opens every time it gets called)
function make_ethogram_plot(datastructure)
% if the figure doesn't exists create it
if(isempty(findobj(0, 'Name', 'My_gramm_ethogram')))
f=figure('Name', 'My_gramm_ethogram');
p1 = uipanel('Parent',f,'BackgroundColor',[1 1 1],'BorderType','none');
g = gramm('x', datastructure.final_data.frameID, 'color', categorical(datastructure.final_data.behavior));
g.geom_raster();
g.set_parent(p1);
g.draw()
else
% defining f,p1, g here (or having them global) works but crashes
% due to refresh rate
g.update()
end
end
I wrote this code to try to replicate your problem:
function animate_random_data
N = 10000;
data = [cumsum(rand(N,1)),randn(N,1)];
for ii=0:1000
% Plot the data
make_ethogram_plot(data);
drawnow
% Compute new data
data(:,1) = cumsum(rand(N,1));
data(:,2) = randn(N,1);
end
function make_ethogram_plot(data)
fig = findobj(0, 'Name', 'My_gramm_ethogram');
if(isempty(fig))
% If the figure doesn't exists create it
fig = figure('Name', 'My_gramm_ethogram');
ax = axes(fig);
plot(ax,data(:,1),data(:,2));
drawnow
set(ax,'xlimmode','manual','ylimmode','manual');
else
% If it does, update it
line = findobj(fig,'type','line');
set(line,'xdata',data(:,1));
set(line,'ydata',data(:,2));
end
Here, I followed your concept of looking for a named figure window, and creating one if it didn't exist. However, if it does exist, I simply replace the XData and YData property of the line that is already there. This is the fastest way of animating a graph, much faster than deleting the existing plot and creating a new one. After plotting, I use drawnow to update the display. I set XLimMode and YLimMode to manual to prevent re-computation of axes limits and consequent re-drawing of the axes.
The function took 17 seconds to draw all 1000 frames, meaning it's drawing about 60 frames a second. It does not (and should not) crash MATLAB.
You can limit the display rate to 20 frames/sec with drawnow limitrate. It will skip updating the display if the frames come too fast.
I don't know what the gramm/update method does, the class is too complicated to quickly see what is going on, but I dare presume it deletes the axes and creates a new plot from scratch. Not that this should crash MATLAB, it might be worth while to submit a bug report. However, you would probably want to update the figure in the more efficient way, following the method I demonstrated above.
Note that this method can be used to update any of the graphical elements in a plot. For example, I have used this method to animate images.
I'm new to MATLAB and I've got a question to ask. I am currently doing up this UI which includes a code which is shown below.
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject handle to Start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global s brightness a t;
t = 0.1
a = 1;
readasync(s);
while (a == 1)
brightness = str2double(fscanf(s, '%s', 8));
set(handles.brightness, 'String', num2str(brightness));
disp(brightness);
if brightness < 87
if brightness < 44
fprintf(s, '%s',1);
else
fprintf(s,'%s',2);
end
else
if brightness < 130
fprintf(s, '%s',3);
else
fprintf(s, '%s',4);
end
end
if(a==0)
break;
end
pause(1/10);
end
As you can see, I am currently using a pause function to delay the while loop. However my mentor in college suggested that I use tic toc instead of pause to delay the loop. I do not know how should I go about with it. He has given me this function but I do not know how to implement it. Any advice?
function delay(t)
tic;
while toc < t
end
end
Where you have pause(1/10), just write delay(1/10) instead. They are not quite the same thing, as pause has a side effect of flushing any graphics updates and GUI callbacks that may have queued up.
As excaza indicated you can replace the line pause(1/10); by delay(1/10); but the function delay must be visible to where it is being called (so for example you can create a file in the same directory called delay.m that contains the code from your mentor).
It might be helpful to point out that replacing the code pause(1/10); by tic; while toc < 1/10 end; accomplishes the same thing functionally, even though this isn't as good from a coding practice/readability perspective.
I am currently evaluating GUIs in MATLAB and was wondering how to maximize a GUI window on startup, without the need for user interaction. The function I am using is stated hereafter and works fine if called on a button press, but calling it in the figure's OpeningFcn won't help.
http://www.mathworks.com/matlabcentral/fileexchange/25471-maximize
Any help on some startup section to place the function call, which is executed after the GUI window has been drawn? I searched for solutions related to startup code in MATLAB GUIs, but there were no results to date.
Thanks in advance for your efforts.
Since many people seem to be interested in this and there is still no public solution, I will describe my approach:
In the YourGUIName_OpeningFcn(hObject, eventdata, handles, varargin) function, append the following lines:
% Initialize a timer, which executes its callback once after one second
timer1 = timer('Period', 1, 'TasksToExecute', 1, ...
'ExecutionMode', 'fixedRate', ...
'StartDelay', 1);
% Set the callback function and declare GUI handle as parameter
timer1.TimerFcn = {#timer1_Callback, findobj('name', 'YourGUIName')};
timer1.StopFcn = #timer1_StopFcn;
start(timer1);
Declare the timer1_Callback and timer1_StopFcn functions:
%% timer1_Callback
% --- Executes after each timer event of timer1.
function timer1_Callback(obj, eventdata, handle)
% Maximize the GUI window
maximize(handle);
%% timer1_StopFcn
% --- Executes after timer stop event of timer1.
function timer1_StopFcn(obj, eventdata)
% Delete the timer object
delete(obj);
Declare the maximize function:
function maximize(hFig)
%MAXIMIZE: function which maximizes the figure withe the input handle
% Through integrated Java functionality, the input figure gets maximized
% depending on the current screen size.
if nargin < 1
hFig = gcf; % default: current figure
end
drawnow % required to avoid Java errors
jFig = get(handle(hFig), 'JavaFrame');
jFig.setMaximized(true);
end
Source of the maximize function:
http://www.mathworks.com/matlabcentral/fileexchange/25471-maximize
I've set WindowButtonMotionFcn to my callback which plots three plots, with the data depending on mouse position. However this seems to be too much for MATLAB to handle, because after moving my mouse around a bit, the GUI stops responding.
I use this code (copied parts from someone):
set(handles.figure1, 'windowbuttonmotionfcn', #hover_Callback);
function hover_Callback(hObject, handles, eventdata)
inside = false;
pos = get(handles.axes1, 'currentpoint');
xlim = get(handles.axes1, 'XLim');
ylim = get(handles.axes1, 'YLim');
if (pos(1,1) > max(xlim(1), 1) && ...
pos(1,1) < xlim(2) && ...
pos(1,2) > ylim(1) && ...
pos(1,2) < ylim(2))
inside = true;
end
if ~inside
return
end
ix = round(pos(1,1));
iy = round(pos(2,2));
axes(handles.axes2); cla; plot(squeeze(t2(ix,iy,:)), squeeze(d2(ix,iy,:)));
axes(handles.axes3); cla; plot(squeeze(t3(ix,iy,:)), squeeze(d3(ix,iy,:)));
axes(handles.axes4); cla; plot(squeeze(t4(ix,iy,:)), squeeze(d4(ix,iy,:)));
This causes my GUI to stop responding, without error codes. If I then quit it and start it again the whole of MATLAB stops responding. Anyone knows what could be happening and how I can fix this? Maybe I'm somehow clogging my memory?
When a callback is called with high frequency, there is the danger that it will be called again before another call has finished executing (i.e. re-entrancy). With WindowButtonMotionFcn, there's a pretty darn good chance that this will happen.
You can prevent callback re-entrancy by inspecting the function call stack (the output of dbstack) for multiple calls to the responsible callback. A very straightforward, but clever implementation of such a check called isMultipleCall is presented in a post on undocumentedmatlab.com. The idea is to count the number of times the callback function name appears on the stack. Take the actual function directly from undocumentedmatlab.com, but it distills to the following:
function flag=isMultipleCall()
s = dbstack();
% s(1) corresponds to isMultipleCall
if numel(s)<=2, flag=false; return; end
% compare all functions on stack to name of caller
count = sum(strcmp(s(2).name,{s(:).name}));
% is caller re-entrant?
if count>1, flag=true; else flag=false; end
The usage of isMultipleCall is very simple. Put run it at the top of the callback (in this case, hover_Callback), and bail out if it indicates that multiple calls are in progress:
function hover_Callback(hObject, eventdata, handles)
if isMultipleCall(); return; end
...
end
This prevents the callback from executing fully again until previous calls have terminated. Only the check will be run, skipping the intensive graphics object operations (i.e. axes, plot, etc.)
An alternative approach is to use a listener for the WindowButtonMotionEvent:
handles.motion = handle.listener(gcf,'WindowButtonMotionEvent',#hover_callback2);
Then in the callback, check the eventdata.CurrentPoint property instead of currentpoint. Check for re-entrancy as above.
If you are NOT using GUIDE and do not have a handles structure managed by guidata, then call the listener something like motionListener and use setappdata to store the listener. For example,
setappdata(hFigure,'mouseMotion',motionListener);
Just use the known handle of any object in the GUI so the listener persists. You could also use UserData instead of setappdata, or any other way of managing GUI data.
As an aside, note that the axes command is rather slow, and can be avoided by passing the axis handle to plot directly:
plot(handles.axes2, squeeze(t2(ix,iy,:)), squeeze(d2(ix,iy,:)));