2017-01-24 6 views
0

GUI/Matlabのタイマーでカウントダウンをコーディングしようとしています。 GUIはガイド付きで作成されます。私の考えは、2つの編集ボックスを持つことです。ここで1つのボックスは、ss:ms(たとえば50.200)の形式で時刻を入力できます。開始ボタンを押すと、2番目の編集ボックスにカウントダウンが表示され、実行されます。 私の問題は現時点では、2番目の編集ボックスにミリ秒ではなく秒を表示できます。私はsprintfを試してみました - しかし、私は、タイマからのハンドル "TasksToExecute"が小数点数を受け入れないと思いますか?編集ボックスに秒とミリ秒を表示する方法はありますか?

誰かが助けることができますか?あるいは誰かが解決する方法を知っていますか?

ありがとうございました。カウントダウンタイマーの文脈において

 function varargout = Countdown_test(varargin) 
%  COUNTDOWN_TEST MATLAB code for Countdown_test.fig 
%  COUNTDOWN_TEST, by itself, creates a new COUNTDOWN_TEST or raises the existing 
%  singleton*. 
% 
%  H = COUNTDOWN_TEST returns the handle to a new COUNTDOWN_TEST or the handle to 
%  the existing singleton*. 
% 
%  COUNTDOWN_TEST('CALLBACK',hObject,eventData,handles,...) calls the local 
%  function named CALLBACK in COUNTDOWN_TEST.M with the given input arguments. 
% 
%  COUNTDOWN_TEST('Property','Value',...) creates a new COUNTDOWN_TEST or raises the 
%  existing singleton*. Starting from the left, property value pairs are 
%  applied to the GUI before Countdown_test_OpeningFcn gets called. An 
%  unrecognized property name or invalid value makes property application 
%  stop. All inputs are passed to Countdown_test_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 Countdown_test 

% Last Modified by GUIDE v2.5 23-Jan-2017 14:47:18 

% Begin initialization code - DO NOT EDIT 
gui_Singleton = 1; 
gui_State = struct('gui_Name',  mfilename, ... 
        'gui_Singleton', gui_Singleton, ... 
        'gui_OpeningFcn', @Countdown_test_OpeningFcn, ... 
        'gui_OutputFcn', @Countdown_test_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 Countdown_test is made visible. 
function Countdown_test_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 Countdown_test (see VARARGIN) 

% Choose default command line output for Countdown_test 
handles.output = hObject; 

% Update handles structure 
guidata(hObject, handles); 

% UIWAIT makes Countdown_test wait for user response (see UIRESUME) 
% uiwait(handles.Countdown_test); 


countdown_timer = timer; 
set(countdown_timer,'ExecutionMode','fixedrate'); 
set(countdown_timer,'Period',1.0); 
set(countdown_timer,'TimerFcn',{@countdowntimercallback,handles}); 

% Store the timer in the GUI so it persists for the life of the GUI 
setappdata(handles.Countdown_test, 'Countdown_timer', countdown_timer); 


fileName = 'state.mat'; 

if exist(fileName) 

    load(fileName) 
    set(handles.ed_PS_Time,'String',state.PST); 

    delete(fileName); 
end 



% --- Outputs from this function are returned to the command line. 
function varargout = Countdown_test_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; 



function ed_countdown_Callback(hObject, eventdata, handles) 
% hObject handle to ed_countdown (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 ed_countdown as text 
%  str2double(get(hObject,'String')) returns contents of ed_countdown as a double 


% --- Executes during object creation, after setting all properties. 
function ed_countdown_CreateFcn(hObject, eventdata, handles) 
% hObject handle to ed_countdown (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 

set(hObject,'String',''); 


% --- Executes on button press in pb_start. 
function pb_start_Callback(hObject, eventdata, handles) 
% hObject handle to pb_start (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 

countdown_timer = getappdata(handles.Countdown_test,'Countdown_timer'); 

anzahl=str2num(get(handles.ed_PS_Time,'String')); 
sprintf('%0.3f',anzahl); 
set(countdown_timer,'TasksToExecute',anzahl); 

start(countdown_timer); 

set(handles.ed_countdown, 'Enable','off'); 

durchlaeufe=get(countdown_timer,'TasksToExecute'); 
setappdata(handles.Countdown_test,'Durchlauf',durchlaeufe); 



function countdowntimercallback(obj,~,handles) 

countdown_timer = getappdata(handles.Countdown_test,'Countdown_timer'); 

durchlaeufe_aktuell=get(countdown_timer,'TasksExecuted'); 

durchlaeufe = getappdata(handles.Countdown_test,'Durchlauf'); 


Countdown = durchlaeufe - durchlaeufe_aktuell; 
sprintf('%0.3f',Countdown); 
set(handles.ed_countdown,'String',Countdown); 

% --- Executes on button press in pb_stop. 
function pb_stop_Callback(hObject, eventdata, handles) 
% hObject handle to pb_stop (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 

% handles.ed_PS_Time = str2num(get(handles.ed_PS_Time, 'String')); 
% guidata(hObject,handles); 
countdown_timer = getappdata(handles.Countdown_test,'Countdown_timer'); 
stop(countdown_timer); 



function ed_PS_Time_Callback(hObject, eventdata, handles) 
% hObject handle to ed_PS_Time (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 ed_PS_Time as text 
%  str2double(get(hObject,'String')) returns contents of ed_PS_Time as a double 

% --- Executes during object creation, after setting all properties. 
function ed_PS_Time_CreateFcn(hObject, eventdata, handles) 
% hObject handle to ed_PS_Time (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 

set(hObject,'String','0'); 




% --- Executes when user attempts to close Countdown_test. 
function Countdown_test_CloseRequestFcn(hObject, eventdata, handles) 
% hObject handle to Countdown_test (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 

% Hint: delete(hObject) closes the figure 
saveState(handles) 
delete(hObject); 

function saveState(handles) 

state.PST = get(handles.ed_PS_Time, 'String'); 

save('state.mat','state') 
} 

答えて

0

TasksToExecuteは、独自に、特に有用な値ではありません。興味のある値は、timerオブジェクトのPeriodです。

例えば

:我々はタイマーがPeriodを差し引く、(最小分解能が0.001秒)ごとPeriod秒を実行することを前提とここ

Yay

:私たちの基本的なGUIを提供します

function honktimer 
% Set up dummy GUI 
h.mainfig = figure('MenuBar', 'none', 'ToolBar', 'None'); 
h.totaltimelbl = uicontrol('Style', 'text', 'Units', 'Normalized', ... 
    'Position', [0.05, 0.80, 0.40, 0.05], 'String', 'Countdown Time (ss.ss)'); 
h.totaltimeeb = uicontrol('Style', 'edit', 'Units', 'Normalized', ... 
    'Position', [0.05, 0.70, 0.40, 0.10], 'String', '1.0'); 
h.periodlbl = uicontrol('Style', 'text', 'Units', 'Normalized', ... 
    'Position', [0.55, 0.80, 0.40, 0.05], 'String', 'Update Rate (Hz)'); 
h.periodbox = uicontrol('Style', 'edit', 'Units', 'Normalized', ... 
    'Position', [0.55, 0.70, 0.40, 0.10], 'String', '10'); 
h.cdlbl = uicontrol('Style', 'text', 'Units', 'Normalized', ... 
    'Position', [0.30, 0.55, 0.40, 0.05], 'String', 'Time Remaining (ss.SS)'); 
h.cdbox = uicontrol('Style', 'edit', 'Units', 'Normalized', ... 
    'Position', [0.30, 0.45, 0.40, 0.10], 'String', '', 'Enable', 'inactive'); 
h.sb = uicontrol('Style', 'pushbutton', 'Units', 'Normalized', ... 
    'Position', [0.30, 0.30, 0.40, 0.10], 'String', 'Start Timer'); 
h.sb.Callback = @(p,e)starttimer(h); % Set callback here so h is fully populated 
end 

function starttimer(h) 
% Set up the timer 
UpdateRate = str2double(h.periodbox.String); % Update rate, Hz 
countdowntime = datetime(h.totaltimeeb.String, 'InputFormat', 'ss.SS'); % Convert countdown time to datetime object 
nupdates = fix(countdowntime.Second*UpdateRate); % Calculate number of timer iterations 
countdown = timer('BusyMode', 'drop', 'ExecutionMode', 'fixedRate', ... 
    'Period', 1/UpdateRate, 'StartDelay', 0, 'TasksToExecute', nupdates, ... 
    'StartFcn', @(p,e)startcountdown(h), ... % Execute on timer start 
    'TimerFcn', @(p,e)updatecountdown(p, h) , ... % Execute each timer update 
    'StopFcn', @(p,e)endcountdown(h)); % Execute when timer ends 

start(countdown); % Start the timer! 
end 

function startcountdown(h) 
h.cdbox.String = h.totaltimeeb.String; 
end 

function updatecountdown(timerobj, h) 
currtime = datetime(h.cdbox.String, 'InputFormat', 'ss.SS'); 
tickduration = seconds(timerobj.Period); 
newtime = currtime - tickduration; 
h.cdbox.String = sprintf('%.2f', newtime.Second); 
end 

function endcountdown(h) 
h.cdbox.String = 'Honk'; 
end 

を現在カウントダウンボックスに表示されている秒数を秒数で更新します。より堅牢なアプローチのために、私はdatetimesecondsを利用して、カウントダウン文字列が小数点以下10進数であると仮定し、str2doubleなどを使用するのとは対照的に、時刻が正確に示されていることを確認しました。

またtimerオブジェクトのUserDataとして総時間を記憶し、各TimerFcn目標時間に基づいて、コール、Period、およびTasksExecuted財産で時間を計算するアプローチで行くことができます。


プログラミングタイマーの通常の警告が適用されます。我々はtimerオブジェクトのTimerFcnが時間の経過に伴って多かれ少なかれ呼び出されていると仮定します。 timerのドキュメントは、ExecutionModeプロパティの説明で実行について少し詳しく説明しています。

関連する問題