Pre-allocating linked arrays for "live" plotting
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
Michelle Hirsch
2011 年 7 月 14 日
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
参考
カテゴリ
Help Center および File Exchange で Graphics Object Programming についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!