Open figure in standalone application

12 visualizaciones (últimos 30 días)
SvB
SvB el 16 de Feb. de 2018
Comentada: Greg el 19 de Feb. de 2018
I have a script controlling a motorized Pan-Tilt Unit (PTU) using a joystick. The way everything is set-up is that control is basically a continuous loop of 1) Check joystick position 2) Send command to PTU according to joystick position. 3) Repeat.
I want to be able to break this loop by pressing a button, so I can properly close all connections. GUIs, callbacks and more were completely new to me yesterday, but I found a simple solution that works in Matlab but not in the stand-alone application. Can anyone point out my mistake? Help is greatly appreciated!
< Note that the joystick control itself works fine (except for a delay, but that's just a minor problem righ tnow). It really is just the figure with stop-button that doesn't show up >
Code that is turned into a stand-alone .exe:
% COMPORT='COM1';
prompt = {'What COMPORT Should be used? Type {COMX} without brackets, replace X by a number'};
dlg_title = 'Comport';
num_lines = 1;
defaultans = {'COM2'};
COMPORT = inputdlg(prompt,dlg_title,num_lines,defaultans);
obj1=StartComms(COMPORT);
% Set PTU message variables
SSS='IP2'; %PTU slave device identifier
MMM='000'; %PTU master controller identifier
JoyID=0; %joystick ID on current machine
RunJoystick(JoyID,SSS,MMM,obj1)
(Note that the Comport dialogue box shows up perfectly!)
Then, the RunJoystick function is of the shape:
function RunJoystick(JoyID,SSS,MMM,obj1)
% Create figure with close button to stop joystick operation
DlgH = figure;
H = uicontrol('Style', 'PushButton', ...
'String', 'Break', ...
'Callback', 'delete(gcbf)');
while ishandle(H)
% Read Joystick position
%[CODE HERE]
% Steer unit if joystick is used
if [Joystick not centered]
[Send move command to PTU]
else %stop unit if joystick is centered
[Send stop command to PTU]
end
end
end
For some reason, the DlgH figure never shows up in the standalone version, while it does when running in Matlab.
If it is of any help, I create the standalone using
mcc -C -W CTF:archive_name_joytest -m SimpleJoystickTest.m
  2 comentarios
Adam
Adam el 16 de Feb. de 2018
Do you get an error when you run it? Either running it from command line or, if you force it to create a log then it will show up in there - I have no idea how to do that from the command line option though as I use the Compiler App instead.
SvB
SvB el 16 de Feb. de 2018
I don't get any error. Just a few minutes ago I added a pause(0.1) behind the figure creation and that seems to fix it... I still think of Matlab only continuing to the next line of code when the previous one has been processed, but apparently that's not entirely true when it comes to figures.
While the figure does appear now, pressing the button doesn't stop the process yet. The quest continues.. :)

Iniciar sesión para comentar.

Respuesta aceptada

Greg
Greg el 16 de Feb. de 2018
Editada: Greg el 16 de Feb. de 2018
MATLAB does only continue to the next line when MATLAB is finished with the line. For graphics commands, MATLAB is done with the line as soon as the OS/GPU fully receives the command to draw to screen. Your pause(.01) gives enough downtime for the graphics queue to "flush" and the figure to appear.
Use drawnow instead. It is specifically designed for this purpose.
You'll probably also need a drawnow at the bottom of the while loop for the delete in the button's callback to register, thus ending the loop.
  4 comentarios
SvB
SvB el 19 de Feb. de 2018
Great, thanks again. That adjustment indeed speeds up the script a little bit, although it does seem to break the "Break-functionality". The only difference I see between your and my code is a few extra inputs in the uicontrol:
DlgH = figure;
H = uicontrol('Style', 'PushButton', ...
'UserData',true,...
'String', 'Break', ...
'Callback', @(obj,~) set(obj,'UserData',false));
drawnow
while H.UserData %I'm running R2017a
%[Joystick Code]
end
close(DlgH)
Should work just fine, shouldn't it? In any case, the gain in framerate (14 Hz rather than 11Hz which is limited by the PTU-readout...) is worth the fact it doesn't close as neat as it could, for now, unless you spot an obvious mistake :)
Greg
Greg el 19 de Feb. de 2018
Sorry for the confusion.
I was intentionally leaving out the pieces you already had, and denoting them with the "..." instances. I realize now that was likely ambiguous because "..." is line continuation.
Your fill-in looks good to me.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by