0

次のよりクリーンなソリューションを知っている人はいますか: ある時点で強制終了する必要がある可能性のある matlab スクリプトを実行しています。「cntrl-C」を押すと機能しますが、デバッグでランダムなファイルが開かれ、図形が描画の途中であるかどうかによっては失敗する可能性があります。

私が思いついた最善の方法は、私が見ている図にボタンを追加し、マウスクリックで「すべてクリア」することです。一部の matlab 関数が正常にキャッチして実行を継続するという例外がスローされるため、単純に「エラー」を実行しても機能しません。

更新 / 明確化: 強制クラッシュは、メイン スクリプトの一部のグローバル変数をクリアすることに依存しています。

function  myScript()
global foo;
foo = 1;
while 1

x = DoStuff();
sh = figure(1);
if k == 1
  killable_window( sh );
end
x.display();
drawnow;
y = foo + 1; % <-- crashes if the callback does 'clear all', which kills global variable foo
end


end

次に、これは killable ウィンドウのダーティ バージョンです。

function [] = killable_window( sh )
  S.fh = sh;
  S.pb = uicontrol('style','push',...
                 'units','pix',...
                 'position',[10 30 80 20],...
                 'fontsize',12,...
                 'string','Quit');          

set(S.pb,'callback'   ,{@pb_call,S})
% Check if 'p' is pressed when focus on button and exec callback
set(S.pb,'KeyPressFcn',{@pb_kpf ,S});

% Check if 'p' is pressed when focus on figure and exec callback
set(S.fh,'KeyPressFcn',{@pb_kpf ,S});

% Callback for pushbutton, clears all variables
function pb_call(varargin)
  S = varargin{3};  % Get the structure.

  fprintf('force quitting due to button press...\n');

  % ghetto: clear everything to force a crash later
  % and prevent anyone from successfully catching an exception
  clear all;
end

% Do same action as button when pressed 'p'
function pb_kpf(varargin)
  if varargin{1,2}.Character == 'p'
      pb_call(varargin{:})
  end
end
end

そのため、表示が気に入らない場合は、「終了」ボタンを押すと、ホーム画面に戻りますが、その過程で変数が失われます...終了する方法、または「エラーを発生させる方法はありますか?」 "誰も例外をキャッチできないようにしますか?

4

1 に答える 1

0

以下は、GUIDE アプリケーション内でコードを整理するのに役立つかもしれません。アプリケーション データ (ここのappdataドキュメントまたはここの一般ドキュメントを参照) を使用して、実行フラグrunflagを作成しました。開始ボタンが押されると、ボタンのコールバック内でメイン ループに入ります。ループは停止ボタンが押されたときに終了し、Figure アプリケーション データのフラグを FALSE に設定します。

これを設定するために使用した手順を次に示します。

  1. 新しい GUIDE アプリケーションを作成することから始めます。
  2. 2 つのボタン (開始と停止) を追加し、
  3. 次のコールバックを定義します

関数コールバックを開く

%# --- Executes just before testStop is made visible.
function testStop_OpeningFcn(hObject, eventdata, handles, varargin)

%# Choose default command line output 
handles.output = hObject;

%# Add a global variable as a MATLAB application data 
%# see sharing application data  documentation in the MATLAB
%# docs 
%# APPDATA documentation:
%#    http://www.mathworks.com/help/techdoc/creating_guis/f13-998352.html#f13-999565
%# General doc on sharing application data
%#    http://www.mathworks.com/help/techdoc/creating_guis/f13-998449.html
%# 
setappdata(handles.figure1,'runFlag',true);

%# Update handles structure
guidata(hObject, handles);

スタートボタンのコールバック

%# --- Executes on button press in btnGo.
function btnGo_Callback(hObject, eventdata, handles)
i=0;

%# This loop will stop when the stop button is pressed
%# setting the application data _runFlag_ to false
while  getappdata(handles.figure1,'runFlag')
    i=i+1;
    %# Work you want to inturrupt can go here.
    pause(.1)
end

停止ボタンのコールバック

%# --- Executes on button press in btnStop.
function btnStop_Callback(hObject, eventdata, handles)            
setappdata(handles.figure1,'runFlag',false)
于 2011-03-03T20:12:46.233 に答える