Why does a TimerFcn fail to operate once another function is called?

I have created a timer to update a waitbar while another funciton is called to read in a data file. The purpose is to show the end user of my GUI that the process is running.
The function that reads in the data file is a mex file and I do not have the ability to update the waitbar directly from it.
I have placed a pause(10) command before the function call to the mex file and the timerfcn works great for those 10 seconds then ceases while the mex file function runs.
Any ideas or reasons why this would happen or ways to fix it would be greatly appreciated.
Thanks

 Réponse acceptée

Walter Roberson
Walter Roberson le 12 Juin 2013
Timers work at the MATLAB level. If MATLAB is not in control, such as if a mex routine has been called (and that routine does not request drawnow()) then the timers cannot work until MATLAB gains control again.
If you have the Parallel Computing Toolkit, you could have the waitbar in one thread and the call to the mex routine in another thread, and you could have the second thread notify the first thread when there was some news about the status of the second thread.
However, that arrangement would not, you will find, be of any benefit in updating the waitbar itself: as long as the mex routine is running, you cannot know how far it has proceeded, cannot know whether perhaps the mex routine got into an infinite loop or got stuck waiting for I/O or waiting for an interrupt that never arrived or got swapped out so that Windows could fly the logo around the screensaver. 15 seconds without return could mean 15 seconds of progress but it might mean no progress at all.
There is, though, a point in waiting the user to be able to cancel an active thread that might happen to be in a mex routine; there is also a point in having "watchdog times" that detect that Something Is Wrong.
The only clean way to force MATLAB activity to top that I have noticed as yet, is to use the Parallel Computing batch() facility, which has a cancel() operation (though I do not know if that can stop something that is already running.)

Plus de réponses (1)

Jan
Jan le 13 Juin 2013
Modifié(e) : Jan le 13 Juin 2013
You can trigger a drawnow from inside the MEX:
mexCallMATLAB(0, NULL, 0, NULL, "drawnow");
You can even start the update of the progressbar directly using mexCallMATLAB.

4 commentaires

I have used a Timer to update a table in guide, and tied its Timerfcn to another function called update_table. running that function through pushbutton works fine, but when i use it as Timerfcn it gives error "conversion from double to cell is not possible". Also if i put a breakpoint where the timer is started, like start(tt), it works well without any error. Have tried changing the period, execution mode busy mode..all of it. How to get around this?
function varargout = DRUM_FULL_V1(varargin)
% DRUM_FULL_V1 M-file for DRUM_FULL_V1.fig
% DRUM_FULL_V1, by itself, creates a new DRUM_FULL_V1 or raises the existing
% singleton*.
%
% H = DRUM_FULL_V1 returns the handle to a new DRUM_FULL_V1 or the handle to
% the existing singleton*.
%
% DRUM_FULL_V1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DRUM_FULL_V1.M with the given input arguments.
%
% DRUM_FULL_V1('Property','Value',...) creates a new DRUM_FULL_V1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before DRUM_FULL_V1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to DRUM_FULL_V1_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 DRUM_FULL_V1
% Last Modified by GUIDE v2.5 16-Jan-2017 12:32:53
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DRUM_FULL_V1_OpeningFcn, ...
'gui_OutputFcn', @DRUM_FULL_V1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(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 DRUM_FULL_V1 is made visible.
function DRUM_FULL_V1_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 DRUM_FULL_V1 (see VARARGIN)
% Choose default command line output for DRUM_FULL_V1
handles.output = hObject;
%------Create timer to update uitable---\\
handles.timer= timer('TimerFcn',@update_table, 'Period', 10.0,'ExecutionMode', 'fixedSpacing')
% timer1.ExecutionMode= 'fixedspacing'
% timer1.Busymode= 'queue'
%--------------------\\
nn= get(handles.figure1);
set(handles.figure1,'name', 'STATION_1')
theta= linspace (0, 2*pi, 1000);
r= (56/2);
x= r*cos(theta);
y= r*sin(theta);
% %figure;
%
% %axis(equal)
% set(handles.axes3,'YTIcklabel', [], 'XTicklabel',[])
% set(handles.axes4,'YTIcklabel', [], 'XTicklabel',[])
% % figure;
% % theta=linspace(0,2*pi)
plot(handles.axes2,x+30,y+30);
axis(handles.axes2,[0 60 0 60]);
if exist('DrumScanningSystem.mdb')
%get database tables in Tabdata
Tabdata= connection_db;
handles.data.Tabdata= Tabdata;
% update the DSSControl Table
ll=Tabdata.SControl;
Gdat= get(handles.uitable4, 'data');
Gdat(1,:)= ll;
set(handles.uitable4, 'data',Gdat);
% update the DSSData table
mm= Tabdata.SData;
Gdat= get(handles.uitable3, 'data');
Gdat(1,:)= mm;
set(handles.uitable3, 'data',Gdat);
else
msgbox('control file absent')
end
if exist('position14.txt')
handles.data.PosData= importdata('position14.txt', ',')
else
msgbox('position file absent')
end
% [X1,Y1]=
%grid(handles.axes2, 'on')
%plot(x, y)
%axes;
% plot (rand(10,10));
% surf(meshgrid(rand(4,4)));
%axis equal;
%gca('grid', 'on');
%[rowC, columnC]= meshgrid(x, y);
%%----%%
%plot(rowC, columnC)
% surf(rowC,columnC);
% view(90,0);
%axes;
% Update handles structureimportdata
guidata(hObject, handles);
%data=guidata(hObject)
% UIWAIT makes DRUM_FULL_V1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = DRUM_FULL_V1_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 pbNext.
function pbNext_Callback(hObject, eventdata, handles)
% hObject handle to pbNext (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close(gcf);
% --------------------------------------------------------------------
function menuOpen_Callback(hObject, eventdata, handles)
% hObject handle to menuOpen (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Pos= handles.data.PosData;
%---test----
%ll= Pos(2:4,4)
% colheaders1 ={'StationID', 'Xposition', 'YPosition', 'Theta', 'Status','Data1','Data2','Data3','Data4'}
% %uitable(gcf,'ColumnName', colheaders)
%
%
% % Set names of columns for DSSControl and DSSData
% set(handles.uitable4, 'Columnname', colheaders)%,'data','Tabdata' )
% set(handles.uitable3, 'Columnname', colheaders)
%set(handles.uitable1, 'data','Tabdata' )
guidata(hObject, handles);
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pbStrtAcq.
function pbStrtAcq_Callback(hObject, eventdata, handles) %--stp by stp drum movement
% hObject handle to pbStrtAcq (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.output= hObject;
%-----------------DATABASE CONNECTION--------
dbpath= strcat(pwd,'\DrumScanningSystem.mdb');%path of working directory
%---maintain this for connection to database---
url = [['jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='''';DBQ='] dbpath];
conn = database('','','','sun.jdbc.odbc.JdbcOdbcDriver',url);
%----------------------------------------------
%-----change values to initial values Line of sight Source Detector
curs= exec(conn,'UPDATE DSSControl SET XPosition= 0, YPosition= 0,Theta= 0 ');
pause(2);
%--reserve for future use----%
iscell(curs.Data) % value 1 shows it is a cell array
%----------------------------%
%---update table with present database values, reflect in table----%
setdbprefs ('DataReturnFormat','cellarray');
curs = exec(conn,'SELECT * FROM DSSControl');
curs = fetch(curs);
SC.SControl = curs.Data
Gdat= get(handles.uitable4, 'data');
Gdat(1,:)= SC.SControl;
set(handles.uitable4, 'data',Gdat);
%-----------------------------------------------------------------=%
%---acess PosData via its SNo--
PosData=handles.data.PosData;
XPOS= num2cell(PosData(:,2)); %need to convert to cell, see help for 'fastinsert'
YPOS= num2cell(PosData(:,3));
THETA= num2cell(PosData(:,4));
exdata(1,:)=[XPOS(2) YPOS(2) THETA(2)] %Each row of exdata to be used with one position command
%sqlArrPos(PosData(:,1))= strcat('UPDATE DSSControl SET XPosition', num2cell(XPOS),'YPOSITION
% colnames= {'XPosition'}%, 'YPosition','Theta'};
% %
% stID= '"1"'
% whereclause= 'WHERE StationID LIKE %1'% CONTAINS',strID)% "1 "';
% data= {8}%,11,11};
%
% update(conn,'DSSControl',colnames,data, whereclause) %cant work without whereclause
%fastinsert
update(conn, 'DSSControl', {'XPosition', 'YPosition', 'Theta'},exdata, 'WHERE StationID LIKE 1')% DO NOT DISTURB THE WHERECLAUSE FOR UPDATION
%curs= exec(conn,'UPDATE DSSControl SET XPosition= 1, YPosition= 1,Theta= 1 ');
close(curs) %close cursor object
close(conn)%close connection
guidata(hObject,handles)
% --- Executes on button press in pbTest.
function pbTest_Callback(hObject, eventdata, handles)
% hObject handle to pbTest (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%msquery
%patch_variables
%hh= findobj(gcbf, 'visible', 'off')
%hh.visible= 'false'
% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%%---use a scheduled function instead of this
%%update_table
%update_table
tt= handles.timer
tt.startdelay= 0.5
start(tt)
% Tabdata= connection_db;
% handles.data.Tabdata= Tabdata
%
%
% % update the DSSControl Table
% ll=Tabdata.SControl;
% Gdat= get(handles.uitable4, 'data');
% Gdat(1,:)= ll;
% set(handles.uitable4, 'data',Gdat);
%
% % update the DSSData table
% mm= Tabdata.SData
% Gdat= get(handles.uitable3, 'data');
% Gdat(1,:)= mm;
% set(handles.uitable3, 'data',Gdat);
%
% %set(handles.uitable1, 'data','Tabdata' )
% guidata(hObject, handles)
% pause(0.5);
%patch_variables;
function update_table(hObject, eventdata, handles)
% update the structure from mdb table into GUI data
%hh= findobj(gcbf, 'tag', 'uitable4')
Tabdata= connection_db;
% % handles.data.Tabdata= Tabdata
%
%
% update the DSSControl Table
ll=Tabdata.SControl
Gdat= get(findobj(gcbf, 'tag', 'uitable4'), 'data')
Gdat(1,:)= ll
set(findobj(gcbf, 'tag', 'uitable4'), 'data',Gdat)
% % update the DSSData table
% mm= Tabdata.SData;
% Gdat= get(findobj(gcbf, 'tag', 'uitable3'), 'data');
% Gdat(1,:)= mm
% set(findobj(gcbf, 'tag', 'uitable3'), 'data',Gdat);
%set(handles.uitable1, 'data','Tabdata' )
%guidata(hObject, handles);
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmp(get(handles.timer, 'Running'), 'on')
stop(handles.timer);
end
delete(handles.timer)
% Hint: delete(hObject) closes the figure
delete(hObject);
I was going to ask you to attach the .m and the .fig and a sample data file (if required), but looking further I can see that the code access a microsoft database; I am not sure I could run that on my Mac at all.
It would also be a good idea to start a new Question for this.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by