Re: MATLAB Code for a stop process button which ex
- From: Ram <sxb4545@xxxxxxxxx>
- Date: Sat, 17 Jun 2006 16:35:55 -0400
Hey Vihang
Thanks for your message..I have done that..but there seems to be an
error Actually the code that is run takes in from multiple files
which is hard for me to send here... but I am sure there seems to
something wrong with the rundatascript which executes a list of files
to be processed once I click the Go button .The dialog box Stop
processing opens up and once the script runs(rundatascript.m) the
stop procesing once clicked doesn't stop the file processing.Instead
it opens up again after a pause delay and all the files gets
processed..It would be nice to have the stop button stop or halt
processing immediately.......the list of files that is in current
execution..I think there is something wrong in the rundatascript
especially in the while loop(for each script command starting in the
h_button=stop_button section....and therefore on...
Secondly How to add a waitbar or what u call progressbar (i.e) I
click on the go processing button and once it process the list of
files the waitbar should appear and be halted once the stop
processing button is clicked...how to go about that....
This is the stop_button code for stop_button fig file...
function varargout = stop_button(varargin)
% STOP_BUTTON M-file for stop_button.fig
% STOP_BUTTON, by itself, creates a new STOP_BUTTON or raises
the existing
% singleton*.
%
% H = STOP_BUTTON returns the handle to a new STOP_BUTTON or the
handle to
% the existing singleton*.
%
% STOP_BUTTON('CALLBACK',hObject,eventData,handles,...) calls
the local
% function named CALLBACK in STOP_BUTTON.M with the given input
arguments.
%
% STOP_BUTTON('Property','Value',...) creates a new STOP_BUTTON
or raises the
% existing singleton*. Starting from the left, property value
pairs are
% applied to the GUI before stop_button_OpeningFunction gets
called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to stop_button_OpeningFcn via
varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows
only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help stop_button
% Last Modified by GUIDE v2.5 11-Nov-2002 13:46:32
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @stop_button_OpeningFcn, ...
'gui_OutputFcn', @stop_button_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before stop_button is made visible.
function stop_button_OpeningFcn(hObject, eventdata, handles,
varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to stop_button (see VARARGIN)
% Choose default command line output for stop_button
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes stop_button wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = stop_button_OutputFcn(hObject, eventdata,
handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global stopFlag;
stopFlag = 1;
THIS IS THE RUNDATASCRIPT CODE
function [dataout, exitcode, scriptmessages] = runDataScript(varargin
)
% runDataScript executes the script commands for a data set
%
% SYNOPSIS:
% A data structure containing a field of matlab commands 'scriptcmds'
% is excecuted flexibly, storing the output data as fields
% in the output structure. It allows exessive processing to be
% delayed from experiment times to later batch processing.
%
% USE:
% [dataout, exitcode] = runDataScript(...input args...)
% dataout: the output data structure
% exitcode: 1 if script executed successfully, -1
% if there were errors, and 0 if execution
% was stopped or there are still scheduled
% commands to run.
% input arguments: Arguments are either single string
% options: -
% 'status' do not execute script but simply display
% the commands. The input data is returned.
% or argument pairs: -
% ... , <option string>,<option value>, ...
% 'data' input data structure
% 'datafile' filename for input data structure in a
% .mat file.
% 'addcmd' add a command to the end of the script.
% 'lines2exe' the line numbers to execute this time.
% (N.B. Ignores whether they have been
% previously executed).
% 'doall' excutes all the lines, scheduling first if
% necessary.
%
% Commands specific to certain field names:
% 'plxfilepath' if there is a field 'plxfiledetails' (see
% below) the existing path will be replaced by
% the given string. This allows the path of files
% to be read in to be changed if the data is
% no longer where it was first specified.
%
% COMMENTS:
% expfundata and dataout are structures. expfundata must contain the
% 'scriptcmds' field. Output variables in the script become members
% of data out. Thus the dataout from excecution of a script
% might contain the following fields:
%
% scriptcmds
% scriptstatus: contains a pair-row for each line:
% [ line_num status ]
% line_num is the order of execution. If line_num is -1,
% it has not been scheduled for execution yet. This allows
% 'inactive' script lines.
% status is: 0 - not yet executed; 1 - executed sucessfully
% -1 or some other negative number - error.
% If scriptstatus does not exist, commands are executed
% in their order of appearance.
% N.B. lines2exe refers to the 'line_num' values, but it
% ignores any status values, executing the line numbers listed
% reagrdless of their status.
%
% This function is designed to facilitate flexible batch processing
% of plexon files. There a number of loosely defined fields. Names
% are not crucial unless specified
% plxfiledetails: the output from getPLXFileDetails
% The contains the fields:
% filename: cell array of file names
% startint: 1st interval in file to collect.
% num_ints: the number of intervals to collect.
% In order for the 'plxfilepath' option to work this must have
% the correct name.
%
% plx: data structure of time stamps and event stamps output from
% loadPLXData
% vars: listof variables associated with each stimulus interval, as
% created by genVars.
% spikes: results of analysing data as spike counts, output from
% PLX2SpikeCount.
% and so on for the output from functions PLX2PSTH, PLX2ISIH etc.
% 07/17/03 - Added code to add
% 'dataname','dataname.fieldname' to parameters of
% display commands. line 367
% 12/22/03 - Updates datafiledetails if they exist so that a backup
is
% created.
fname = 'runDataScript';
% ----- Constants and default values for variables -------
saveflag = 1;
doallflag = 0;
displayonlyflag = 0;
nodisplayflag = 0;
lines2exe = [];
plxfilepath = [];
datafilein = [];
dataname = [];
statusonlyflag = 0;
exitcode = 1;
expfundata =[];
noof_newcmds = 0;
newcmds = {};
scheduleflag = 0;
scriptmessages = {};
msgno = 0;
dataout = struct;
startdir = pwd;
datadir = startdir;
global stopFlag;
global scriptstatus;
% ------ Check input arguments and set up options ------
i=1;
while i<=nargin
if isstr(varargin{i})
switch varargin{i}
% 'plxfilepath' - allows new path to be set up in a field
% 'plxfiledetails'
case 'plxfilepath'
i=i+1;
plxfilepath = varargin{i};
% 'lines2exe' - lines to execute - a list of line numbers
case 'lines2exe'
i=i+1;
lines2exe = varargin{i};
case 'doall'
doallflag = 1;
case 'display'
displayonlyflag = 1;
case 'nodisplay'
nodisplayflag = 1;
% 'addcmd' - add a command, to the end of the list.
case 'addcmd'
i=i+1;
noof_newcmds = noof_newcmds + 1;
newcmds{noof_newcmds} = varargin{i};
% 'status' - displays the script and the status of each line,
% but does not execute any. If a file input was specified
% the structure is handed out in dataout - thus can also
% be used to load the data.
case 'status'
statusonlyflag = 1;
% Load up the data from file
case 'datafile'
i=i+1;
if isAFile(varargin{i})
fprintf('Loading up data from file %s.\n',varargin{i});
datafilein = varargin{i};
[p n e] = str2Filename(datafilein);
if ~isempty(p)
cd(p);
datadir = p;
end;
datafilein = [pwd '\' n '.' e];
expfundata = load(datafilein,'-MAT');
else
fprintf([fname ':data file ' varargin{i} ' not
found.\n']);
exitcode = 0;
end;
case 'dontsave'
saveflag = 0;
% Data comes in as a structure
case 'data'
i=i+1;
expfundata = varargin{i};
dataname = inputname(i);
end;
i=i+1;
end;
end;
% Check if there is expfundata, then....
if ~isempty(expfundata)
% ----- expand out the expfundata variables to their own titles.
----
datafieldnames = fieldnames(expfundata);
noof_datafieldnames = length(datafieldnames);
for i=1:noof_datafieldnames
eval([ datafieldnames{i} ' = expfundata.' datafieldnames{i}
';' ]);
end;
% Check for script commands
if isfield(expfundata,'scriptcmds')
noof_scriptlines = length(scriptcmds);
else
noof_datafieldnames = noof_datafieldnames + 1;
scriptstatus = [];
scriptcmds = {};
noof_scriptlines = 0;
datafieldnames{noof_datafieldnames} = 'scriptcmds';
end;
% ------- If there is no scriptstatus field create one. -----
if ~isfield(expfundata,'scriptstatus')
scriptstatus = [ 1:length(scriptcmds);
zeros(1,length(scriptcmds)) ]';
noof_datafieldnames = noof_datafieldnames+1;
datafieldnames{noof_datafieldnames} = 'scriptstatus';
end;
else
datafieldnames = {'scriptcmds','scriptstatus'};
noof_datafieldnames = 2;
scriptcmds = {};
scriptstatus = [];
noof_scriptlines = 0;
end;
% If there is no scriptmessages field create one
if isempty(who('scriptmessages'))
noof_datafieldnames = noof_datafieldnames + 1;
datafieldnames{noof_datafieldnames} = 'scriptmessages';
end;
% Messages are always emptied before each execution.
scriptmessages = {};
% ---------- Check for a script at least -------------
if isempty(scriptcmds) & noof_newcmds==0
msgno = msgno+1;
scriptmessages{msgno} = [fname ': no script to execute'];
warning(scriptmessages{msgno});
exitcode = 1;
return;
end;
% ----- execute absolutely everything - schedules all commands ------
if doallflag
unscheduledlines = find(scriptstatus(:,1)<1);
maxlinesofar = max(scriptstatus(:,1));
scriptstatus(unscheduledlines,1) =
[maxlinesofar+1:maxlinesofar+length(unscheduledlines)]';
end;
% -------- Add any new commands ---------------------------
for i=1:noof_newcmds
noof_scriptlines = noof_scriptlines+1;
scriptcmds{noof_scriptlines} = newcmds{i};
if noof_scriptlines>1
scriptstatus(noof_scriptlines,:) = [ max(scriptstatus(:,1))+1
0];
else
scriptstatus(noof_scriptlines,:) = [1 0];
end;
end;
% --------- display only - only run those scheduled display commands
------
if displayonlyflag
lines2exe = scriptstatus(find( scriptstatus(:,1)>0 ),1)';
displaycmds = [];
for i=1:length(lines2exe)
if
~isempty(findstr(lower(scriptcmds{lines2exe(i)}),'display'))
displaycmds = [displaycmds lines2exe(i)];
end;
end;
lines2exe = displaycmds;
end;
% -------- Prepare thelist of which commands to execute ------
% Currently very simple. If you don't specify the lines, it does
% them exactly.
if isempty(lines2exe)
lines2exe = scriptstatus(find( scriptstatus(:,2)<1 &
scriptstatus(:,1)>0 ),1)';
end;
% ---------do not run any scheduled display commands ------
if nodisplayflag
notdisplaycmds = [];
for i=1:length(lines2exe)
if
isempty(findstr(lower(scriptcmds{lines2exe(i)}),'display'))
notdisplaycmds = [notdisplaycmds lines2exe(i)];
end;
end;
lines2exe = notdisplaycmds;
end;
% --------- check to see if there were any prior errors
--------------------
if ~isempty( find( scriptstatus(:,2)== -1 ) )
msgno = msgno+1;
scriptmessages{msgno} = [fname ': Previous errors found.'];
warning(scriptmessages{msgno});
exitcode = 0;
end;
% --------- Display the list of commands to be run. -----------
fprintf('runDataScript: listing script...\n');
linenumbers = sort(lines2exe);
for i=linenumbers
lineindex = find(scriptstatus(:,1)==i);
fprintf('%d: %s\n',i,scriptcmds{lineindex});
end;
% ------------ Check for plxfilepath option - execute if exists.
-----------
if ~isempty(plxfilepath) & isfield(expfundata,'plxfiledetails')
for j=1:plxfiledetails.noof_files
[p n e] = str2FileName(plxfiledetails.filename{j});
if plxfilepath(end)~=slash4OS
plxfilepath(end+1) = slash4OS;
end;
plxfiledetails.filename{j} = [plxfilepath n '.' e];
end;
end;
% ------------ Check for statusonly flag - end routine. -----------
if statusonlyflag
if noof_newcmds
expfundata.scriptcmds=scriptcmds;
expfundata.scriptstatus=scriptstatus;
end
dataout = expfundata;
return;
end;
% display the stop button.
hstop_button = stop_button;
pause(0.1); % forces the stop button
% ------ Loop thru each command line in the script that needs to be
executed ----
stopFlag = 0;
index = 1;
if ~isempty(linenumbers)
i = linenumbers(index);
while index<=length(linenumbers) & ~stopFlag
% Optimistic setting of command status.
commandstatus = 1;
% Get the next command to execute
lineindex = find(scriptstatus(:,1)==i);
cmdstr = scriptcmds{lineindex};
msgno = msgno+1;
scriptmessages{msgno} = sprintf('Executing line %d:
%s',i,cmdstr);
fprintf('%s\n',scriptmessages{msgno});
% Get the apparent data field name:
equalind = findstr(cmdstr,'=');
fntmps = {};
if isempty(equalind)&& stopFlag
% If there is no output field name
msgno = msgno+1;
scriptmessages{msgno} = [fname ': No output produced from
command.'];
warning(scriptmessages{msgno});
stop(hstop_button);
%exitcode = 0;
else
% Get the field name(s) - handles multiple names
fntmps = getIdentifiers(cmdstr(1:equalind-1))
% If the output field names do not already exist - add it
to the
% field names
for k=1:length(fntmps)
fntmp = fntmps{k};
olddataflag = 0;
for j = 1:length(datafieldnames)
if strcmp(datafieldnames{j}, fntmp)
msgno = msgno+1;
scriptmessages{msgno} = [fname ': existing
data field ''' fntmp ''' will be overwritten.'];
warning(scriptmessages{msgno});
olddataflag = 1;
%exitcode = -1;
end;
end;
if ~olddataflag
noof_datafieldnames = noof_datafieldnames + 1;
datafieldnames{noof_datafieldnames} =
sprintf('%s',fntmp);
end;
end;
end;
% Run any checks
% --- no checks ----!
% Here check to see what the command is
% and if it is a display command, then add
% paramater 'dataname' to it including the
% dataname if datafilein is empty.
oldcmd=[];
if length(scriptcmds{lineindex})>6&& ...
strcmp('display',lower(scriptcmds{lineindex}(1:7)))...
&&~isempty(dataname)&&isempty(findstr(scriptcmds{lineindex},'''datanam
e'''))
% newcmd=strrep(lower(scriptcmds{lineindex}),'gui(',...
% ['gui(' dataname '.'])
first_paren=findstr('(',scriptcmds{lineindex});
first_comma=findstr(',',scriptcmds{lineindex});
field_name=scriptcmds{lineindex}(first_paren+1:first_comma-1);
newcmd=strrep(scriptcmds{lineindex},[field_name ','],...
[field_name ',''dataname'',''' dataname '.'
field_name ''',']);
oldcmd=scriptcmds{lineindex};
scriptcmds{lineindex}=newcmd;
end
% Execute the command
eval(scriptcmds{lineindex},'commandstatus=-1; exitcode=-1;')
;
if ~isempty(oldcmd)
scriptcmds{lineindex}=oldcmd;
end
% If it fails
if commandstatus==-1
[lastmsg lastid] = lasterr;
scriptmessages{msgno+1} = [fname ': command failed'];
warning(scriptmessages{msgno+1});
scriptmessages{msgno+2} = [lastmsg '; ' lastid];
msgno=msgno+2;
% remove any datafieldname
if ~isempty(equalind)
noof_datafieldnames = noof_datafieldnames - 1;
datafieldnames =
{datafieldnames{1:noof_datafieldnames}};
% eval([datafieldnames{noof_datafieldnames} '=
{lastmsg lastid};']);
end;
end;
% Store the execution status
scriptstatus(lineindex,2) = commandstatus;
% Manage the option to stop at this point
if index<length(linenumbers)
pause(5); % pauses to give the user a chance to
find the
% stop button.
i = linenumbers(index+1);
end;
index = index +1;
end;
if any(strcmp(datafieldnames,'datafiledetails'))
datafiledetails.backup=1;
datafiledetails.date_modified=now;
datafiledetails.modified=1;
end
else
msgno = msgno+1;
scriptmessages{msgno} = ['No processing to do.'];
fprintf('No processing to do.');
end;
close(hstop_button); % close the stop button
% --------- If there was no output, stop now ------
if isempty(datafieldnames)
msgno = msgno+1;
scriptmessages{msgno} = [fname ': No data output produced'];
warning(scriptmessages{msgno});
%exitcode = 0;
return;
end;
% ------- If the processing was not finished --------
if index<=length(linenumbers)
msgno = msgno+1;
scriptmessages{msgno} = [fname ': Execution of datascript
interrupted by user.'];
fprintf('%s:the data script was interrupted before it finished
processing.\n', ...
fname);
fprintf('To resume processing execute runDataSctipt on the output
data or file.\n');
end;
% -------- possibly set up exit code to 0 to indicate there is more
to do ---------
cmdsnotdone = find( scriptstatus(:,1)>0 & scriptstatus(:,2)==0 );
if ~isempty(cmdsnotdone) & exitcode
exitcode = 0;
end;
% ------- Construct the data output ----------------
cmdstr = [];
for i=1:length(datafieldnames)
if i>1
cmdstr = [cmdstr ','];
end;
if eval(['iscell(' datafieldnames{i} ')'])
cmdstr = [cmdstr 'datafieldnames{' num2str(i) '},{'
datafieldnames{i} '}'];
else
cmdstr = [cmdstr 'datafieldnames{' num2str(i) '},'
datafieldnames{i}];
end;
end;
dataout = eval(['struct(' cmdstr ')']);
if (~isempty(datafilein) | saveflag) & ~displayonlyflag
if ~isempty(datafilein)
dataout=savePLXFile(dataout,datafilein);
else
dataout=savePLXFile(dataout);
end;
msgno = msgno+1;
scriptmessages{msgno} = sprintf('Wrote data to file
%s.',dataout.datafiledetails.filename);
fprintf('%s\n',scriptmessages{msgno});
cd(startdir);
end;
if ~isempty(dataname)
assignin('caller',dataname,dataout);
end;
% -- Display a message if an error was encountered. ---
switch exitcode
case -1,
warning([fname ': Problem encountered executing script.']);
case 0,
warning([fname ': Commands remain unexecuted.']);
end;
function schedule
% This function shcedules all the as yet unscheduled routines
% for execution. It is called as a script command.
global scriptstatus;
unscheduledind = find( scriptstatus(:,1) == 0 | scriptstatus(:,1)
== -1);
maxlinesofar = max(scriptstatus(:,1));
linenums = [maxlinesofar+1 : maxlinesofar+length(unscheduledind)];
scriptstatus(unscheduledind,1) = linenums';
.
- References:
- Prev by Date: Simulink Sinewave-buffer scope problem
- Next by Date: Re: Getting least sqaure error from mldivide
- Previous by thread: Re: MATLAB Code for a stop process button which executes and halt the process of running data files in between or at anytime
- Next by thread: Display the value of the point on the graphic
- Index(es):