Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4023726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:38:02+00:00 2026-05-20T10:38:02+00:00

Does anyone know of a cleaner solution for the following: I’m running a matlab

  • 0

Does anyone know of a cleaner solution for the following: I’m running a matlab script that might need to be killed at some point. Hitting “cntrl-C” works but pops open some random file in debug, and might still fail depending on if the figures are in the middle of drawing.

Best I could come up with: add a button to the figure I’m looking at, on mouse-click do “clear all”. Simply doing “error” doesn’t work because it throws an exception that some matlab function successfully catches and continues running.

Update / Clarification: the force-crash relies on clearing some global variable in the main script.

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

Then this is the dirty version of a killable window:

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

so, if I don’t like what I see, I hit the “quit” button, and it dumps back to home screen, but I lose my variables in the process… is there a way to quit, or make “error” prevent anyone from catching the exceptions ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:38:02+00:00Added an answer on May 20, 2026 at 10:38 am

    Perhaps the following might help you organize your code within a GUIDE application. I have used application data (see the appdata docs here or the general docs here) to create a execution flag runflag. Once the start button is pressed the main loop is entered within the button’s callback. The loop terminates when the stop button is pressed setting which sets the flag in the figures application data to FALSE.

    Here are steps I used to set this up.

    1. Start by creating a new GUIDE application,
    2. add two buttons (Start and Stop) and
    3. define the following callbacks

    Opening Function Callback

    %# --- 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);
    

    Start button call back

    %# --- 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
    

    Stop Button Callback

    %# --- Executes on button press in btnStop.
    function btnStop_Callback(hObject, eventdata, handles)            
    setappdata(handles.figure1,'runFlag',false)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of any (free) tools that will convert an old Delhi 5
Does anyone know is there a better way to check if some image contain
Does anyone know the function of the command vmware-vim-cmd vimsvc/connect (vimid). I thought that
Does anyone know what this error means? I've read that it could be PHP
Does anyone know how to change icons in Silverlight outofbrowser application?
Does anyone know if views states (used in Silverlight) will be available in next
Does anyone know how to add an item to a list but do it
Does anyone know of a "similar words or keywords" algorithm available in open source
Does anyone know of an equivalent to FxCop/StyleCop for Delphi? I would really like
Does anyone know how I can go about hiding the tab selectors for the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.