Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
Curious
Posts:
1,700
Registered:
12/6/04
|
|
Re: preallocating matrix
Posted:
Feb 8, 2013 3:24 PM
|
|
"Ayush Patawari" <ayush_patawari@yahoo.com> wrote in message <kf2iu2$okh$1@newscl01ah.mathworks.com>... > Hi, > > My matlab code is getting stuck because of the different lengths of the vectors. here is the code: > > plot_timer=0; > > ax_array=[]; > ay_array=[]; > az_array=[]; > timearray=[]; > > %%%PORT OPERATIONS > port_num = get(handles.com,'String'); > set(handles.com,'Style','text'); > s=serial(port_num); > set(s, 'InputBufferSize', 256); %number of bytes in inout buffer > set(s, 'FlowControl', 'hardware'); > set(s, 'BaudRate', 115200); > set(s, 'Parity', 'none'); > set(s, 'DataBits', 8); > set(s, 'StopBit', 1); > set(s, 'Timeout',10); > > > disp(get(s,'Name')); > prop(1)=(get(s,'BaudRate')); > prop(2)=(get(s,'DataBits')); > prop(3)=(get(s, 'StopBit')); > prop(4)=(get(s, 'InputBufferSize')); > > disp(['Port Setup Done!!',num2str(prop)]); > > fopen(s); > set(handles.status,'String','Connected'); > > while(1) > start_timer=tic; > out = fscanf(s); > C = textscan(out, '%f %f %f' ,'delimiter',','); > [a1x,a1y,a1z] = deal(C{:}); > > end_timer=toc(start_timer); %End timer > plot_timer=plot_timer+end_timer; > > [n,w]=buttord(0.2 ,0.3 ,5 ,20); > [b,a]=butter(n,w); > movavg=25; > amov=1; > bmov=ones(1,movavg)./movavg; > > %Accelerometer1 > a1x=a1x-450; > a1x=a1x/19; > a1xf=filter(b,a,a1x); > a1xf=filter(bmov,amov,a1xf); > > > a1y=a1y-450; > a1y=a1y/19; > a1yf=filter(b,a,a1y); > a1yf=filter(bmov,amov,a1yf); > > > a1z=a1z-450; > a1z=a1z/19; > a1zf=filter(b,a,a1z); > a1zf=filter(bmov,amov,a1zf); > > %Angle calculation > a1pitch=atan2(a1xf,sqrt(a1yf.^2+a1zf.^2)); > a1pitch=a1pitch/pi*180; > > a1roll=atan2(a1yf,sqrt(a1xf.^2+a1zf.^2)); > a1roll=a1roll/pi*180; > > a1yaw=atan2(a1zf,sqrt(a1xf.^2+a1yf.^2)); > a1yaw=a1yaw/pi*180; > > > ax_array=[ax_array;a1roll]; > ay_array=[ay_array;a1pitch]; > az_array=[az_array;a1yaw]; > > timearray=[timearray;plot_timer]; > > axes(handles.graph1) > plot(timearray,ax_array) > axis auto; > grid on; > hold on; > xlabel('Time[s]'); > ylabel('Roll'); > drawnow; > > > ===>> this code is written for a matlab GUI which accepts values from accelerometer and plots the various axis angles real time. But when i run this code it shows that the vector in "ax_array=[ax_array;a1roll];" is not of the same length. there is also error in "plot(timearray,ax_array)". Please suggest me some solution. > > Ayush
For statements like:
ax_array=[ax_array;a1roll]; ay_array=[ay_array;a1pitch]; az_array=[az_array;a1yaw];
to work, the length of all the rows of a*_array must be the same as the length of the corresponding a1roll, a1pitch, and a1yaw vectors.
From the above code, I'm not sure where your while(1) loop ends, but I'm guessing (at least) the first time through the loop, the above condition is not met since the a*_array is empty.
One workaround is to assign the a*_array to the corresponding a1roll, a1pitch, and a1yaw values the first time through the loop, such as :
ax_array=a1roll; ay_array=a1pitch; az_array=a1yaw;
then use your original syntax for subsequent times through the loop (assuming the length of the vectors don't change. (Same applies to timearray as well.)
|
|
|
|