2

I am trying to send data from my arduino to matlab and use the GUI. I want to read continously the data even when no button is pressed. In order to so, i have to use the fscanf function but i don't know where to put it. There is definitely a while loop that waits the events(such as a pushed button) in which this function should be placed. I am just a beginner so this might be a silly question for you. Thank you in advance!

function varargout = UltraPlot(varargin)

global a;
global k;
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
               'gui_Singleton',  gui_Singleton, ...
               'gui_OpeningFcn', @UltraPlot_OpeningFcn, ...
               'gui_OutputFcn',  @UltraPlot_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
disp('Ultraplot');


function UltraPlot_OpeningFcn(hObject, eventdata, handles, varargin)

global a;
global k;
a = serial('COM3');
fopen(a);
handles.output = hObject;


guidata(hObject, handles);


disp('Opening');


function varargout = UltraPlot_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;
global a;
global k;
disp('varargout');

function Start_Callback(hObject, eventdata, handles)

global a;
global k;
fprintf(a,'%d',1);
disp('Button pressed');

1 Answer 1

2

You have to set Matlab to wait for any data from Arduino in a while loop, check this sample code :

clear;clc;
S=serial('com18'); % Create an S Object
data=0;
set(S,'inputbuffersize',4096,'timeout',20); % Set serial communication     parameter
fopen(S); % Open serial communcation

   while (1)
        if s.bytesavailable>0 % If data from Arduino is available
          data=fscanf(S);
          data = str2num(data);
          % Do whatever you want with data here...
        end
     data=0;
   end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.