Open figure in standalone application

11 visualizzazioni (ultimi 30 giorni)
SvB
SvB il 16 Feb 2018
Commentato: Greg il 19 Feb 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 Commenti
Adam
Adam il 16 Feb 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 il 16 Feb 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.. :)

Accedi per commentare.

Risposta accettata

Greg
Greg il 16 Feb 2018
Modificato: Greg il 16 Feb 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 Commenti
SvB
SvB il 19 Feb 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 il 19 Feb 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by