簡體   English   中英

在x軸上帶有計時器的matlab中繪制

[英]plot in matlab with timer on x axis

我有一個Matlab代碼,它針對循環迭代繪制變量值。

僅在此示例中,我就將隨機值視為變量值。

function varargout = myplot(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @myplot_OpeningFcn, ...
                   'gui_OutputFcn',  @myplot_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 myplot is made visible.
function myplot_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;
% Update handles structure
guidata(hObject, handles);


function varargout = myplot_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes1);
iif = 0;
i = 1;
y = 0;
while (i < 1000)
    yf = rand(1);
    y = [y, yf];
    iif = [i,iif];
    i = i + 1;
    plot(iif,y);
    pause(0.001);
end

如何替換iif = [i,iif]; 進入計時器?

我要擁有的只是針對時間(以秒為單位)繪制的數據,而不是數據與循環迭代的關系。 有人有什么主意嗎? 謝謝。

您可以使用tictoc跟蹤經過的秒數。 tic啟動計數器,對toc所有后續調用都返回自上次tic以來經過的秒數。

iif = 0;
i = 1;
y = 0;

hplot = plot(iif, y);
xlabel('Elapsed Time (s)')
ylabel('Value')

% Force a graphics refresh so that isn't counted in our elapsed time
drawnow

% Start the timer
tic

while (i < 1000)
    yf = rand(1);
    y = cat(2, y, yf);

    % Call toc to get the current elapsed time
    iif = cat(2, iif, toc);

    i = i + 1;

    set(hplot, 'xdata', iif, 'ydata', y);
    pause(0.001)
end

在此處輸入圖片說明

附帶說明,您還可以將循環編寫為for循環,並預分配所有變量,以提高性能。

nIterations = 1000;
iif = nan(nIterations, 1);
y = nan(nIterations, 1);

hplot = plot(iif, y);

tic

for k = 1:nIterations
    y(k) = rand(1);
    iif(k) = toc;

    set(hplot, 'XData', iif, 'YData', y)
    pause(0.001)
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM