在MATLAB GUI中创建tic / toc向量

时间:2015-08-17 20:03:27

标签: matlab user-interface matlab-guide

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

1 个答案:

答案 0 :(得分:2)

最强大的解决方案是在GUI的句柄结构中存储和操作向量。首先,在你的"创建函数"中,初始化开始和停止向量:

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

接下来,在每个其他按钮回调中,从句柄结构中检索开始时间并确定已用时间:

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

最后,您可以使用"输出功能"从GUI输出值。

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

然后,您将在命令行中使用以下语句执行您的gui:

>> [timeStart,timeStop] = yourGui ( );
相关问题