Accelerometer Data Processing Tool - Matlab Gui
Accelerometer Data Processing Tool - Matlab Gui
DATA PRO C ES S I N G
TO O L
PROJETP08A08: ACCELEROMETRES & FER ROVIAIRE
07/01/2009
JIHANE NASSEH
Table of Content
Introduction ........................................................................................... 4
1. Visual Portion of the GUI .................................................................. 5
2. Add Files and Delete Files Callback Functions ................................ 6
3. Processing the Data............................................................................ 8
3.1 Start pushbutton Callback ............................................................................... 8
3.2 Disable Buttons and enable Buttons functions ............................................. 9
3.3 Convert acceleration to g function ................................................................ 10
3.4 Calibration function ....................................................................................... 12
3.5 Mean function ................................................................................................ 13
3.6 Plot Data function .......................................................................................... 13
Introduction
This tutorial will provide you with information about our data processing GUI
and all the functions that were implemented in it. After reading it, you will
understand how we implemented all the functions and be able to make an
eventual modification or development. Following, you will find the visual
aspect of the GUI, and the code to each component callback. This tutorial draws upon
many of the basic GUI elements: adding files to a listbox, parsing data, plotting data onto
the GUI, saving GUI plots, disabling/enabling buttons, exporting data to Excel format, and
many other things.
This tutorial is written for those with a good amount of experience creating a Matlab GUI.
If
you
are
new
to
creating
GUIs
in
Matlab,
you
should
visit
this
website:
www.blinkdagger.com, which provides a clear introduction to Matlab and GUIs and contains
further explanations of the basic GUI elements. Basic/Advanced knowledge of Matlab is
highly recommended. Matlab version R2006a is used in writing this tutorial. Both earlier
versions and new versions should be compatible as well (as long as it isnt too outdated).
First, download the GUI from the USB allocated to our project. Unzip the files and
place them wherever you please.
2.
3.
Choose to open the GUI by clicking on Open Existing GUI. Click on Browse to
locate where you saved the GUI files.
4.
Here is what the GUI should look like when you open it:
5.
Click on the
23.
24.
25.
26.
27.
28.
29.
30.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
To allow multiple files to be selected within the listbox, the following code was added to
the opening function. Alternatively, you can change these properties through the Property
Inspector using GUIDE.
set(handles.inputFiles_listbox,'Max',2);
set(handles.inputFiles_listbox,'Min',0);
To make sure that it work in the GUI. You can try adding/removing files to the GUI. The
menu below pops up when you click on the Add Files button.
idea to
14. %review this by using the Matlab Help Files
15. handles.data = {};
16. handles.legendData = {};
Distinction of motion
Terrestrial axis
26. [d,p,h]=calibration(a);
27. t=a(:,1);
activity
from
the
gravity
in
the
m/s2
29. handles.data{x}=[t, acceleration]; % storing all the data in
=
plotData(handles.data,handles.legendData,handles.axes1,get(hand
les.plot_popupmenu,'Value'));
31. %the data must be done processing before other Callbacks will
be
32. %able to function properly. this variable will be used as a
"check"
33. %to see whether the data has been processed or not
34. handles.processDataCompleted = 1;
35. %data is done processing, so re-enable the buttons
36. enableButtons(handles);
37. guidata(hObject, handles);
3.2 Disable Buttons and enable Buttons functions:
The first thing you might notice is the disableButtons and enableButtons functions.
Basically, these functions are included so that while Matlab is busy processing the data,
the user cannot click on any of the other buttons. The code for the two functions is:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
function disableButtons(handles)
set(handles.figure1,'Pointer','watch');
set(handles.start_pushbutton,'Enable','off');
set(handles.reset_pushbutton,'Enable','off');
set(handles.addFiles_pushbutton,'Enable','off');
set(handles.savePlot_pushbutton,'Enable','off');
set(handles.deleteFiles_pushbutton,'Enable','off');
set(handles.inputFiles_listbox,'Enable','off');
set(handles.plot_popupmenu,'Enable','off');
set(handles.export_pushbutton,'Enable','off');
function enableButtons(handles)
set(handles.figure1,'Pointer','arrow');
set(handles.start_pushbutton,'Enable','on');
set(handles.reset_pushbutton,'Enable','on');
set(handles.addFiles_pushbutton,'Enable','on');
set(handles.savePlot_pushbutton,'Enable','on');
set(handles.deleteFiles_pushbutton,'Enable','on');
18.
19.
20.
set(handles.inputFiles_listbox,'Enable','on');
set(handles.plot_popupmenu,'Enable','on');
set(handles.export_pushbutton,'Enable','on');
function anew=convert_accele_to_g(data)
m=0;
j=0;
sw=0;
%the input to this function is the file name of the data file.
%If the data file is not in the current Matlab directory
%you must include the entire directory path.
%opens the file
fid2 =fopen(data);
while feof(fid2) == 0
data2=fgetl(fid2);
remain2 = data2;
if (size(remain2)==[1 31]) %All the valide frames have the size
[1 31]
j=j+1;
[Heure, remain2] = strtok(remain2,',');
H(j)=str2double(Heure);
[Minute, remain2] = strtok(remain2,',');
Mn(j)=str2double(Minute);
[Seconde, remain2] = strtok(remain2,',');
S(j)=str2double(Seconde);
t1(j)=(H(j)*3600)+(Mn(j)*60)+S(j);%time of traveling per
second
t(j)=t1(j)-t1(1);%time calibration
%Extraction of acceleration on x axis
[trame, remain2] = strtok(remain2,'');
m=m+1;
if size(trame)==[1 19]
datax1_8(m)=bin2dec(trame(4));
datax2_8(m)=bin2dec(trame(5));
x_dec(m)=((256*datax1_8(m))+datax2_8(m)); %Decimal
value relatively to axis x on 16 bits
%Extraction of acceleration on y axis
datay1_8(m)=bin2dec(trame(7));
10
datay2_8(m)=bin2dec(trame(8));
y_dec(m)=((256*datay1_8(m))+datay2_8(m));
value relatively to axis y on 16 bits
%Decimal
%Decimal value
else
n=n+1;%number of wrong values (just for information)
x_dec(m)=x_dec(m-1); %%Decimal value relatively to axis y
on 16 bits
y_dec(m)=y_dec(m-1);
z_dec(m)=z_dec(m-1);
for k=1:m
%Compute of batterys voltage
Vbat(k)=((65536/brp_dec(k))*1.20);
ADCres(k)=Vbat(k)/65536;
%Compute of the output voltage on each axis
Vxout(k)= x_dec(k)*ADCres(k);
Vyout(k)= y_dec(k)*ADCres(k);
Vzout(k)= z_dec(k)*ADCres(k);
%conversion to g (9.81 m/s2)
Vdd=3.2;
S=0.8;
ax1(k)=(1/S)*(Vxout(k) - (Vdd/2));
ay1(k)=(1/S)*(Vyout(k) - (Vdd/2));
az1(k)=(1/S)*(Vzout(k) - (Vdd/2));
end
temps=t';
a=[temps ax1' ay1' az1']; %Time in secondes, ax, ay, az in g
11
the horizontal component of the dynamic acceleration, for more information about
1.
function[d,p,h]=calibration(data)
6.
7.
8.
[nl nc]=size(a);
13.
14.
end
end
15.
16.
17.
h=d-p;
% We compute the horizontal component of the dynamic
acceleration by vector subtraction
18.
return
function[data_x,data_y,data_z]=mean(data,n)
2.
3.
4.
data_x=0;
data_y=0;
data_z=0;
5.
for i=2:n
6.
data_x=data_x+ data(i,2);
12
7.
8.
9.
10.
11.
12.
data_y=data_y+ data(i,3);
data_z=data_z+ data(i,4);
end
data_x= data_x/(n-1);
data_y= data_y/(n-1);
data_z= data_z/(n-1);
13.
return
function [legendObject]=plotData(data,legendData,axesName,option)
cla(axesName); %clear the axes
axes(axesName); %set the axes to plot
hold on
grid on
%plot the acceleration plot
if (option==1)
for x=1:(length(data))
plot(data{x}(:,1),data{x}(:,2),'r');
hold on;
plot(data{x}(:,1),data{x}(:,3),'c');
hold on;
plot(data{x}(:,1),data{x}(:,4)),'g';
hold off;
end
%add a legend to the plot
legendObject = legend(legendData,'Location','Best');
title('Acceleration')
xlabel('Time (S)')
ylabel('Acceleration (m/s2)');
%plot speed plot
elseif (option==2)
for x=1:(length(data))
[speed]=Integration(data{x},0,0,0,0,0,0);
acceleration to get the speed
%integration
of
plot(data{x}(:,1),speed(:,2),'r');
hold on;
plot(data{x}(:,1),speed(:,3),'c');
hold on;
plot(data{x}(:,1),speed(:,4)),'g';
hold off;
end
%add a legend to the plot
legendObject= legend(legendData,'Location','Best');
title('Speed')
xlabel('Time(s)')
ylabel('Speed (m/s)');
13
legendObject= legend(legendData,'Location','Best');
title('Position')
xlabel('Time(s)')
ylabel('Position (m)');
end
hold off
%allow legend titles to be displayed properly
set(legendObject,'Interpreter','none');
As you could notice, to be able to display the speed and position, it was necessary to call
the Integration function, that has the following code:
function[speed,position]=Integration(acceleration,X0,Y0,Z0,V0_X,V0_Y,
V0_Z)
[nl nc]=size(acceleration);
%Initial parameters
position(1,2)=X0;
position(1,3)=Y0;
position(1,4)=Z0;
speed(1,2)=V0_X;
speed(1,3)=V0_Y;
speed(1,4)=V0_Z;
%Speed vector
for i=2:nl
for j=2:4
t(i,1)= acceleration(i,1)- acceleration(i-1,1);
speed(i,j)=acceleration(i,j)*t(i,1)+speed(i-1,j);
end
end
%Displacement vector
for i=2:nl
for j=2:4
t(i,1)= (acceleration(i,1)- acceleration(i-1,1));
position(i,j)=0.5*acceleration(i,j)*t(i,1)^2+speed(i1,j)*t(i,1)+position(i-1,j);
end
14
end
return
The pop-up menu is relatively easy to use and allows the user to select between
displaying the acceleration plot, speed plot or the position plot on the axes. You will find
the following code on plot_popupmenu_Callback
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
15
5-
end
%adjusts the new figure accordingly
set(newFig,'Units',axes_units);
set(newFig,'Position',[15 5 axes_pos(3)+30 axes_pos(4)+10]);
%saves the plot
saveas(newFig,fullfile(pathname, filename))
%closes the figure
close(newFig)
16
And to erase those empty sheets when the Excel file is created, we can use the following
function:
function deleteEmptyExcelSheets(fileName)
%this function erases any empty sheets in an excel document
%the input fileName is the entire path of the file
%for example, fileName = 'C:\Documents and Settings\matlab\myExcelFile.xls'
excelObj = actxserver('Excel.Application');
%opens up an excel object
excelWorkbook = excelObj.workbooks.Open(fileName);
worksheets = excelObj.sheets;
%total number of sheets in workbook
numSheets = worksheets.Count;
count=1;
for x=1:numSheets
%stores the current number of sheets in the workbook
%this number will change if sheets are deleted
temp = worksheets.count;
%if there's only one sheet left, we must leave it or else
%there will be an error.
if (temp == 1)
17
break;
end
%this command will only delete the sheet if it is empty
worksheets.Item(count).Delete;
%if a sheet was not deleted, we move on to the next one
%by incrementing the count variable
if (temp == worksheets.count)
count = count + 1;
end
end
excelWorkbook.Save;
excelWorkbook.Close(false);
excelObj.Quit;
delete(excelObj);
The following figure shows how the excel document with the exported data should look
like:
18
First, lets deal with the opening function. Here, you will find the standard tool bar
and the close GUI confirmation dialog.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
%%
function
varargin)
data_processing_tool_OpeningFcn(hObject,
eventdata,
handles,
handles.output = hObject;
set(hObject,'toolbar','figure'); %enables toolbar
%this variable used to prevent users from breaking the GUI
%the variable is set to 1 once the data has been processed
handles.processDataCompleted = 0; %
%this command asks the user to confirm closing of GUI
set(handles.figure1,'CloseRequestFcn','closeGUI');
% Update handles structure
guidata(hObject, handles);
%%
function closeGUI
selection = questdlg('Do you want to close the GUI?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(gcf)
case 'No'
return
end
7. Reset Button
Now, for the reset button. Resetting your GUI to the default state can save the user a lot
of time and fustration. Instead of closing and opening the GUI to get to the starting state,
the user can simply click on the reset button.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
19
28.
29.
30.
31.
set(handles.inputFiles_listbox,'String','');
set(handles.inputFiles_listbox,'Value',0);
%updates the handles structure
guidata(hObject, handles);
20