Open figure in standalone application

8 ビュー (過去 30 日間)
SvB
SvB 2018 年 2 月 16 日
コメント済み: Greg 2018 年 2 月 19 日
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 件のコメント
Adam
Adam 2018 年 2 月 16 日
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 2018 年 2 月 16 日
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.. :)

サインインしてコメントする。

採用された回答

Greg
Greg 2018 年 2 月 16 日
編集済み: Greg 2018 年 2 月 16 日
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 件のコメント
SvB
SvB 2018 年 2 月 19 日
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 2018 年 2 月 19 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by