Accelerometer data MatLab GUI

Hello i'm trying to read in raw data from an accelerometer thru my matlab GUI. I have a pushbutton for serial read, calibration, and port closing that all work fine but i'm trying to create a button that will read in raw data from the accelerometer port.
% START DATA - PUSHBUTTON6
function pushbutton6_Callback(hObject, eventdata, handles)
function [gx gy gz] = readAcc(out,calCo)
%mapping between analog inputs and X,Y,Z axes.
% Xch = 1;
% Ych = 3;
% Zch = 2;
fprintf(out.s,'R');
%read voltages from accelerometer and reorder
reordered(1)= fscanf(out.s,'%u');
reordered(2)= fscanf(out.s,'%u');
reordered(3)= fscanf(out.s,'%u');
%determine what offset and gain values to use
offset = calCo.offset;
gain = calCo.g;
accel = (reordered - offset) ./ gain;
%map analog inputs to axes
gx = accel(1);
gy = accel(2);
gz = accel(3);
end
guidata(hObject, handles);
The error i'm getting is
Error: File: BW2.m Line: 161 Column: 1 The function "readAcc" was closed with an 'end', but at least one other function definition was not. To avoid confusion when using nested functions, it is illegal to use both conventions in the same file.
even if I delete the bottom 'end' function, I press the pushbutton and nothing reads out.

9 Comments

The push button calls for pushbutton6_Callback(), right? But you don't do anything inside it. Either call for readAcc() from the pushbutton6_Callback(), or preferrebly set the handle for the pushBotton callback to your readAcc, i.e.
set(pbHandle, 'Callback', {@readAcc, arg1, arg2})
But it's probably better to take away the declaration of readAcc() all together.. I suggest to keep the data in the handles variable instead. And for future notice, dont call guidata(hObject, handles) if you don't change the handles. It's just unnecessary.
Edit: I never answered the Error code.. If you start putting end on functions, you will need it on all functions. Just take away the "end" and you're all good
% START DATA - PUSHBUTTON6
function pushbutton6_Callback(hObject, eventdata, handles)
set(handles.pushbutton1, 'UserData', 0);
function [gx gy gz] = readacc(out,calCo)
%mapping between analog inputs and X,Y,Z axes.
% Xch = 1;
% Ych = 3;
% Zch = 2;
fprintf(out.s,'R');
%read voltages from accelerometer and reorder
reordered(1)= fscanf(out.s,'%u');
reordered(2)= fscanf(out.s,'%u');
reordered(3)= fscanf(out.s,'%u');
%determine what offset and gain values to use
offset = calCo.offset;
gain = calCo.g;
accel = (reordered - offset) ./ gain;
%map analog inputs to axes
gx = accel(1);
gy = accel(2);
gz = accel(3);
guidata(hObject, handles);
Is that what you're looking for? I'm confused. Was I suppose to state
set(pbHandle, 'Callback', {@readAcc, arg1, arg2})
in the opening function?
Ryan G
Ryan G on 1 Feb 2013
Edited: Ryan G on 1 Feb 2013
You don't need to 'end' a function in MATLAB gui workflow. Everytime you create a new function the previous one is automatically ended.
The problem is the only thing you call when you hit the pushbutton is:
set(handles.pushbutton1, 'UserData', 0);
You never process the readacc function because it needs to be called in hte pushbutton callback.
No, I ment
function pushbutton6_Callback(hObject, eventdata, handles)
%mapping between analog inputs and X,Y,Z axes.
% Xch = 1;
% Ych = 3;
% Zch = 2;
fprintf(out.s,'R');
%read voltages from accelerometer and reorder
reordered(1)= fscanf(out.s,'%u');
reordered(2)= fscanf(out.s,'%u');
reordered(3)= fscanf(out.s,'%u');
%determine what offset and gain values to use
offset = calCo.offset;
gain = calCo.g;
accel = (reordered - offset) ./ gain;
%map analog inputs to axes
handles.gx = accel(1);
handles.gy = accel(2);
handles.gz = accel(3);
guidata(hObject, handles)
Notice that the readacc() function is gone, and you've put the data into handles instead. Not a must, but my suggestion.
Observe that you might need to initialise the handles.gx, .gy and .gz in your GUI's main_OpeningFcn(), found in the beginning of the .m-file of the GUI (if you've been using GUIDE, that is). I.e.
function main_OpeningFcn(hObject, eventdata, handles)
...
handles.gx= 0;
handles.gy= 0;
...
You can then access the variables from anywhere in the gui by the handles.
In case you don't already know this, there is an Accepted Answer button for solutions that helped you, and the possibility to plus up runner ups. This greatly helps other's who's in the same need as yourself.
Hi yes, sorry I was in class. I receive the following error: Undefined variable "out" or class "out.s".
Error in BW2>pushbutton6_Callback (line 132) fprintf(out.s,'R');
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in BW2 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)BW2('pushbutton6_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
>>
for this code
function pushbutton6_Callback(hObject, eventdata, handles)
%mapping between analog inputs and X,Y,Z axes.
% Xch = 1;
% Ych = 3;
% Zch = 2;
fprintf(out.s,'R');
%read voltages from accelerometer and reorder
reordered(1)= fscanf(out.s,'%u');
reordered(2)= fscanf(out.s,'%u');
reordered(3)= fscanf(out.s,'%u');
%determine what offset and gain values to use
offset = calCo.offset;
gain = calCo.g;
accel = (reordered - offset) ./ gain;
%map analog inputs to axes
handles.gx = accel(1);
handles.gy = accel(2);
handles.gz = accel(3);
guidata(hObject, handles)
and in the openingfnc
guidata(hObject, handles);
handles.gx= 0;
handles.gy= 0;
Because it doesn't know what variable handle out.s is. Wherever you initiate out, initiate it to the handles variable instead. Again, doing so allows you to fetch that data from anywhere in the gui. I.e.
handles.out= "your current out.s"
guidata(hObject, handles)
Amed
Amed on 1 Feb 2013
Edited: Amed on 1 Feb 2013
I'm confused sorry, but what is my current out.s?
edit: I have this in my openingfnc guidata(hObject, handles); handles.out = out.s; handles.gx= 0; handles.gy= 0;
and recieved this error Undefined variable "out" or class "out.s".
Error in BW2>BW2_OpeningFcn (line 61) handles.out = out.s;
Error in gui_mainfcn (line 221) feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in BW2 (line 42) gui_mainfcn(gui_State, varargin{:});
>>
Yes, it's the same problem as my last comment......
If you yourself do not know what out.s is, I cant help you. I guess it's the output from you sensor...

Sign in to comment.

Answers (1)

ASHISHREDDY THUMMALA
ASHISHREDDY THUMMALA on 14 Apr 2019
You have declared 2 functions in your script, then you should have end for both functions. That means two end's
What accelerometer are you using ?

Asked:

on 1 Feb 2013

Community Treasure Hunt

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

Start Hunting!