在 MATLAB GUI 中创建 tic/toc 向量

Creating tic/toc vector in MATLAB GUI

我使用 Matlab 中的 GUIDE 功能构建了一个 GUI。我有很多按钮。我首先单击一个按钮开始 tic。然后,当我单击任何其他按钮时,我想构建一个 toc 时间戳向量。我该怎么做呢?

最可靠的解决方案是在 GUI 的句柄结构中存储和操作矢量。首先,在你的 "create function" 中,初始化开始和停止向量:

function yourGui_CreateFcn ( hObject , eventdata , handles )
  % Initialize the start and stop vectors.
  handles.timeStart = [];
  handles.timeStop = [];

  % Update the GUI handles structure.
  guidata ( hObject , handles );
end

然后,在您的第一个按钮中,启动计时器并将其存储到您的句柄向量中。

function button1_Callback ( hObject , eventdata , handles )
  % Start the timer, updating the value in the handles structure.
  handles.timeStart = tic;

  % Update the GUI data so that timer is available to other functions.
  guidata ( hObject , handles );
end

接下来,在每个其他按钮回调中,从 handles 结构中检索开始时间并确定经过的时间:

function button2_Callback ( hObject , eventdata , handles )
  % Retrieve the start time.
  timeStart = handles.timeStart;

  % Determine the elapsed time.
  timeElapsed = toc ( timeStart );

  % Store the new value in the handles structure.
  handles.timeStop(end+1,1) = timeElapsed;

  % Update the guidata.
  guidata ( hObject , handles );
end

最后,您可以使用 "output function" 从 GUI 输出值。

function yourGui_OutputFcn ( hObject , eventdata , handles )
  % Specify the output variables.
  varargout { 1 } = handles . timeStart;
  varargout { 2 } = handles . timeStop;
end

然后您将在命令行中使用以下语句执行您的图形用户界面:

>> [timeStart,timeStop] = yourGui ( );