Pre-allocating linked arrays for "live" plotting

3 vues (au cours des 30 derniers jours)
Will
Will le 5 Juil 2011
I am currently developing a GUI matlab program to plot and analyze real time data from a serial port. I have a serial object with a BytesAvailableFcn that calls every time a terminator character is received. This callback then parses the data and adds it to an array linked to a graph. The graph is then updated at regular intervals using a timer.
My question is how should I pre-allocate the data arrays linked to the graph. Some of the things I have tried are below.
If I did not have to consider performance I could just resize the array every time I get new data.
x = []
y = []
set(plot_handle, 'XDataSource', 'x', 'YDataSource', 'y')
function serial_callback
% get data from serial obj
% parse data
% add data to plot arrays
x = [x x_data]
y = [y y_data]
end
function timer_callback
refreshdata
drawnow
end
for obvious memory reasons this gets inefficient very quickly. I have also tried pre-allocating the array as Nan's and adding data to the first Nan.
x = nan(200)
y = nan(200)
set(plot_handle, 'XDataSource', 'x', 'YDataSource', 'y')
function serial_callback
% get data from serial obj
% parse data
% add data to plot arrays
index = find(isnan(x),1); % using a 'pointer' instead of the find command would speed this up but you get the idea
x(index) = x_data
y(index) = y_data
end
function timer_callback
refreshdata
drawnow
end
this works in that the space is presumably pre-allocated and data points I don't have yet don't get plotted. It however messes with the legend. I don't exactly know how the color of legend symbols is calculated in graphs with Cdata (which I am also using) but the Nan's make the legend symbols disappear.
I have also considered setting the linked array equal to an increasingly large part of a pre-allocated array assuming matlab will only copy the references as long as I don't modify the linked data but this seems to be getting very complex. Is there an easier way to ask for a large empty array that I can add plot data to.

Réponses (1)

Michelle Hirsch
Michelle Hirsch le 14 Juil 2011
I think the issue might simply be with your call to nan. You are creating 200 x 200 arrays of NaNs for x and y. I imagine your intention is:
x = nan(200,1);
I've done this same type of thing many times, and have built some plotting widgets to make it easier. You might find the utilities I put on the File Exchange to be helpful: Test and Measurement Widgets
  1 commentaire
Will
Will le 14 Juil 2011
Thanks for the response, you are correct but I believe my original code was nan(200,1). I missed that simplifying it for this post. I don't think it would have plotted if x and y were matrices instead of vectors. So far I have been solving this problem by pre-allocating with numbers outside my plotting range, which works because it is a scatter plot but it still does not seem like a great solution.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Objects dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by