0

I have a Matlab code which plots the variable value against the loop iterations.

I have considered random values as variable value just for this example.

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

How can I replace iif = [i,iif]; into a timer ?

All I want to have is data plotted against the time(in seconds) instead of data vs loop iterations. Anyone have any idea ? Thanks.

cppiscute
  • 707
  • 2
  • 10
  • 33

1 Answers1

0

You can use tic and toc to keep track of the seconds that have elapsed. tic starts the counter and all successive calls to toc return the seconds that have elapsed since the last 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

enter image description here

As a side note, you could also write your loop as a for loop and pre-allocate all of your variables which will improve performance.

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
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Thanks, I just had to replace iif = [toc , iif]; – cppiscute Mar 24 '16 at 16:04
  • Thanks for the suggestion, but its a minimal example of a very large code. – cppiscute Mar 24 '16 at 16:05
  • @statisticalbeginner Understandable. Just remember that re-allocation of data by dynamically expanding an array can be *very* costly especially when you're talking about thousands of iterations. – Suever Mar 24 '16 at 16:08
  • I have just realised that It made the performance worst, because of the concatanation i am doing. I am losing 2 seconds there, and its not at all acceptable in the implementation i am doing. I will think about time reducing methods. thanks – cppiscute Mar 24 '16 at 16:17
  • @statisticalbeginner Do you know how many iterations you will be doing? Alternately a common alternative is to expand the matrix to double it's size each time you have to expand it. – Suever Mar 24 '16 at 16:18
  • Thanks I will increase the pre-allocation size and I will check. – cppiscute Mar 24 '16 at 16:19
  • Number of iterations varies based on the frequency content of the audio sample. to give an example, a file with 15 sec duration can run for about 400 iterations roughly. – cppiscute Mar 24 '16 at 16:23
  • @statisticalbeginner Yea you can always just initialize to 2000 elements or something higher than what you need. As long as they are NaN, they won't show up in your plots. – Suever Mar 24 '16 at 16:24