timer function and handles... again...

4 次查看(过去 30 天)
powerarcs
powerarcs 2014-11-18
评论: Geoff Hayes 2014-11-18
Hi guys,
to start this off, I am quite new to MATLAB... I may be missing some very simple points here, but I could not find any solution... I am aware, that there are quite a few posts about timers and proper handles usage already. My problem is not be covered (correct me if I'm wrong) by any of them.
I want to have a serial temperature logger, which upon registering a temperature over a certain threshold, triggers another measurement.
Therefor I use the following timer, checking the temperature once per second:
tempTimer = timer( ...
'Name', 'tempTimer', ...
'ExecutionMode', 'fixedRate', ...
'Period', 1, ...
'TimerFcn', { @controlTemp, handles } );
the controlTemp function looks (simplified) as follows:
function controlTemp( ~, ~, handles )
%some variable declarations...
if currentTemp >= setTemp
WPRread( handles );
else
% some other stuff...
end
end
The problem is occuring, if the WPRread( handles ) function is called:
Error while evaluating TimerFcn for timer 'tempTimer'
Invalid file identifier. Use fopen to generate a valid file identifier.
If i simply display some message instead of calling WPRread(handles) all goes fine, and WPRread(handles) is also working fine if triggered by the callback of a button in the GUI.
I have been fiddling around for serveral hours now without finding out why exactly this goes south... so any help would be greatly appreciated!
Cheers, powerarcs

回答(1 个)

Orion
Orion 2014-11-18
Hi,
It seems to me that the problem is inside the data handles.
I don't know your function WPRread, but, when you say WPRread(handles) is also working , do you mean that you tested it outside the gui ?
the error you get Invalid file identifier. is often due to the use of function such as fopen, fprintf,...
so, when you call WPRread inside the gui code, do you pass the same input arguments as when you call it from outside ?
I guess that one of your structure field (ex : handles.FileName) is wrong and this is the source of the error.
you should put a breakpoint at the line
WPRread( handles );
end see if the data handles contains all the necessary and valid data.
  1 个评论
Geoff Hayes
Geoff Hayes 2014-11-18
Following what Orion said, remember that when you create your timer as tempTimer = timer(...,'TimerFcn', { @controlTemp, handles } ); you are passing in a copy of handles as it exists when the timer is created. And so on every subsequent "firing" of the timer (so when controlTemp is called) it will be using the copy of handles from when the timer was created and NOT any updated version of it.
What I sometimes do, when my timer needs the updated handles structure, I will pass the figure/GUI handle (usually handles.figure1 if using GUIDE to create your GUI and you haven't changed the name/tag of the figure) in place of handles as
tempTimer = timer( ...
'Name', 'tempTimer', ...
'ExecutionMode', 'fixedRate', ...
'Period', 1, ...
'TimerFcn', { @controlTemp, handles.figure1 } );
Then, in the controlTemp callback, I would use the figure handle to get handles as
function controlTemp( ~, ~, figHandle )
% get the latest copy of handles
handles = guidata(figHandle);
%some variable declarations...
if currentTemp >= setTemp
WPRread( handles );
else
% some other stuff...
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Code Execution 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by