Arduino Read In Matlab LSM9DS0 Multiple Lines

Hi everyone,

I'm trying to write a code to plot the read in from LSM9DS0, which is a 9DOF IMU with an accelerometer, magnetometer, and gyroscope.

I'm using a GUI to display the graphs, and I'm recycling code from an older file, where I graphed the voltage and current of various electrodes, so ignore the titles on the GUI.

The error message that I'm getting is:

Reference to non-existent field 'accelx'.

Error in lsm9Ds0code (line 10)
DATA.accelx = [DATA.accelx, y1];

Error in instrcb (line 36)
feval(val{1}, obj, eventStruct, val{2:end});

Warning: The BytesAvailableFcn is being disabled. To enable the callback property
either connect to the hardware with FOPEN or set the BytesAvailableFcn property.

I'm also having an issue because I'm trying to read in the code from the serial monitor on the arduino, but it prints out strangely (see attached image).

I'm not sure why I keep getting this error, unless its incorrectly reading in the numbers from the serial monitor.

My code "pushbuttons.m" is for the GUI and "lsm9Ds0code" is the function file to read in the arduino.

pushbuttons.m:

function varargout = pushbuttons(varargin)
% PUSHBUTTONS MATLAB code for pushbuttons.fig
%      PUSHBUTTONS, by itself, creates a new PUSHBUTTONS or raises the existing
%      singleton*.
%
%      H = PUSHBUTTONS returns the handle to a new PUSHBUTTONS or the handle to
%      the existing singleton*.
%
%      PUSHBUTTONS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PUSHBUTTONS.M with the given input arguments.
%
%      PUSHBUTTONS('Property','Value',...) creates a new PUSHBUTTONS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before pushbuttons_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to pushbuttons_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help pushbuttons

% Last Modified by GUIDE v2.5 25-Jun-2014 15:07:15

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @pushbuttons_OpeningFcn, ...
                   'gui_OutputFcn',  @pushbuttons_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before pushbuttons is made visible.
function pushbuttons_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to pushbuttons (see VARARGIN)

% Choose default command line output for pushbuttons
handles.output = hObject;


% Update handles structure
guidata(hObject, handles);
%close 

global arduino Axes1 Axes2

Axes1 = handles.axes1;
Axes2 = handles.axes2;

delete(instrfindall);
arduino = serial('COM3');
% arduino = serial('/dev/tty.usbmodemfd121');
set(arduino,'BaudRate',9600);
arduino.BytesAvailableFcn= @lsm9Ds0code;




% UIWAIT makes pushbuttons wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = pushbuttons_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;






% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


global arduino Axes1 Axes2 DATA

DATA=[];
DATA.counter = [];
% DATA.reference = [];
% DATA.working = [];
% 
% DATA.currentPD = [];
% DATA.current = [];

fopen(arduino)
cla(Axes1);
cla(Axes2);



% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global arduino
% arduino.BytesAvailableFcn = '';
% pause(.1);

fclose(arduino)

lsm9DsOcode:

function lsm9Ds0code(~,~,~,~)
global arduino Axes1 Axes2 DATA %sample coeffsample avgy
if strcmp(arduino.status,'open')
a = str2mat(fscanf(arduino));
a1 = [a(10) a(11) a(12) a(13)];

y1 = str2num(a1)

 
DATA.accelx = [DATA.accelx, y1];
% DATA.reference = [DATA.reference, y2];
% DATA.working = [DATA.working, y3];
% 
% DATA.currentPD = [DATA.currentPD, y4];
% DATA.current = [DATA.current, y5];

 x = 1:length(DATA.accelx);
%  x2 = 1:length(DATA.current);
 
 
% if(~isempty(DATA.counter))
% counter(end);
% end
% if(~isempty(current))
% current(end);
% end
% % 
% % sample = 10;
% % coeffsample = ones(1, sample)/sample;
% % avgy = filter(coeffsample, 1, y);
% % 
% % if length(y) <100
% %     plot(Axes1,x,avgy);
% % else
% %     plot(Axes1,x(end-99:end),y(end-99:end),'LineWidth',3); %hold(Axes1,'on');
% %  plot(Axes1,x(end-99:end),avgy(end-99:end),'r','LineWidth',3); hold(Axes1, 'off');
% %   s = std(y(end-99:end));
% %   a = std(avgy(end-99:end));
% % %    %b = s/a
%    plot(Axes1, x, DATA.counter, 'LineWidth',3);
%    hold(Axes1,'on');
%    plot(Axes1, x, DATA.reference, 'r', 'LineWidth',3);
%    plot(Axes1, x, DATA.working, 'g', 'LineWidth', 3);
%    hold(Axes1, 'off');
%    pause(.005);
%    
% 
%    plot(Axes2, x2, DATA.currentPD, 'LineWidth', 3);
%    hold(Axes2,'on');
%    plot(Axes2, x2, DATA.current, 'r', 'LineWidth', 3)
%    hold(Axes2, 'off');

end

end

Any help would be greatly appreciated!

right now i'm really just trying to read in one number, the x value for the accelerometer, to make sure the communication is working.

Lara