How to dynamically increase the size of the array in Matlab

32 views (last 30 days)
I am trying to plot 2D map using below code. The values are taken from serial port. The problem I am facing is that I don't know how many data points my robot will give. The below code has static memory ie. It can hold 10 values. But when robot moves it may give 1000 datapoints. So I want the array to be dynamic and terminate when string such as 'stop' comes from serial monitor . But I don't know how to do it. Please help me on this. Thanks in advance.
% Create a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM3', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM3', 'BaudRate',9600,'Terminator','LF');
%obj1.BaudRate=9600
else
fclose(obj1);
obj1 = obj1(1);
end
% Connect to instrument object, obj1.
fopen(obj1);
% Communicating with instrument object, obj1.
Data1 = [1:10];%can hold 10 values
Data2 = [1:10];
Data3= [1:10];
Data4= [1:10];
for i=1:10
Data1(i) = str2double(query(obj1, '*IDN?'));
Data2(i) = str2double(query(obj1, '*IDN?'));
Data3(i)= str2double(query(obj1, '*IDN?'));
Data4(i)=str2double(query(obj1, '*IDN?'));
end
x = [Data2];
y =[Data1];
a =[Data3];
b=[Data4];
h = linspace(-pi,pi);
y1 = sin(h);
hold on
plot(x,y);
plot(h,y1)
plot(a,b);
%axis([-50 50 -50 50])
hold off
fclose(obj1);
% Clean up all objects.
delete(obj1);

Accepted Answer

Walter Roberson
Walter Roberson on 6 May 2019
In MATLAB, if you assign a value into an array at a location past the current size of the array, then MATLAB will extend the array.
However, beyond roughly 50 or so elements in the array, what MATLAB has to do to extend the size of an array is to create a new array that is large enough to hold all of the information, and then to copy the information from the old location to the new one, and then get rid of the old location. If you have a loop like
for K = 1 : n
Data1(K) = K;
end
then this involves N*(N+1)/2 memory accesses, due to the need to copy on every assignment. This is acceptable for small array sizes, but it quickly adds up.
There are multiple techniques to allocate memory more efficiently.
One of the techniques is to allocate enough memory to hold the largest array that you might ever need, and then when you turn out to use less, to throw away the extra.
Another technique is to allocate in batches. For example you might allocate in groups of 1 Kb (128 doubles), and each time you find you get to the end of what you have allocated, extend the array by another 1 Kb. This still does copying, but far less often.
Another technique is to use cell arrays, as cell arrays do not need to copy the contents of the cells when they get extended, just the pointers to them. But it still has the copying issues discussed above. And each cell entry has about 104 bytes of overhead.
  3 Comments
Walter Roberson
Walter Roberson on 6 May 2019
maxN = 10000;
Data1 = zeros(1,maxN);
Data2 = zeros(1,maxN);
Data3 = zeros(1,maxN);
Data4 = zeros(1,maxN);
and then your code.
And then after your code,
Data1 = Data1(1:i-1);
Data2 = Data2(1:i-1);
Data3 = Data3(1:i-1);
Data4 = Data4(1:i-1);

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!