Dynamic Display in edit text box
2 views (last 30 days)
Show older comments
I am integrating arduino with Matlab, which involves the use of ir sensor(TRCT 500). I am displaying the voltage read of the analog pin for ir sensor. So instead of pressing push button to get the varying outputs, i want it to be automatic. I tried with the timefcn but dont know how to apply it here.

function varargout = ir_sensor1(varargin)
% IR_SENSOR1 MATLAB code for ir_sensor1.fig
% IR_SENSOR1, by itself, creates a new IR_SENSOR1 or raises the existing
% singleton*.
%
% H = IR_SENSOR1 returns the handle to a new IR_SENSOR1 or the handle to
% the existing singleton*.
%
% IR_SENSOR1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IR_SENSOR1.M with the given input arguments.
%
% IR_SENSOR1('Property','Value',...) creates a new IR_SENSOR1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ir_sensor1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ir_sensor1_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 ir_sensor1
% Last Modified by GUIDE v2.5 17-Oct-2022 20:59:06
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ir_sensor1_OpeningFcn, ...
'gui_OutputFcn', @ir_sensor1_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 ir_sensor1 is made visible.
function ir_sensor1_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 ir_sensor1 (see VARARGIN)
% Choose default command line output for ir_sensor1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ir_sensor1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ir_sensor1_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;
clear all;
global a;
a = arduino('COM5');
% --- 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 a;
voltage = 100*readVoltage(a,'A0');
set(handles.valtxt,'String',voltage);
if voltage <= 90
set(handles.racetext,'String','None');
else
set(handles.racetext,'String','No bitches');
end
function valtxt_Callback(hObject, eventdata, handles)
% hObject handle to valtxt (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of valtxt as text
% str2double(get(hObject,'String')) returns contents of valtxt as a double
% --- Executes during object creation, after setting all properties.
function valtxt_CreateFcn(hObject, eventdata, handles)
% hObject handle to valtxt (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function racetext_Callback(hObject, eventdata, handles)
% hObject handle to racetext (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of racetext as text
% str2double(get(hObject,'String')) returns contents of racetext as a double
% --- Executes during object creation, after setting all properties.
function racetext_CreateFcn(hObject, eventdata, handles)
% hObject handle to racetext (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
0 Comments
Answers (1)
Satyam
on 6 Jun 2025
Hi Brendan,
To automatically and continuously update the IR sensor reading (without pressing the button each time), a ‘timer’ object is better suited than ‘timefcn’ for GUI-based applications in MATLAB. Refer to the documentation of ‘timer’ class to know about related arguments and their syntax. https://www.mathworks.com/help//releases/R2021a/matlab/ref/timer-class.html
Here is a sample code of how we can achieve that:
Modify the ‘ir_sensor1_OpeningFcn’ function to add ‘timer’ object initialization as follows:
function ir_sensor1_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for ir_sensor1
handles.output = hObject;
% Initialize Arduino
global a;
a = arduino('COM5'); % Update port if needed
% Set up timer for automatic reading
handles.t = timer('ExecutionMode', 'fixedSpacing', ...
'Period', 1, ...
'TimerFcn', {@updateSensor, hObject});
start(handles.t); % Start timer
% Update handles structure
guidata(hObject, handles);
‘pushbutton1_Callback’ function needs to be replaced by ‘updateSensor’ function. Here is the sample code for the same:
function updateSensor(~, ~, hObject)
handles = guidata(hObject);
global a;
% Read sensor
voltage = 100 * readVoltage(a, 'A0');
% Update GUI text
if isfield(handles, 'valtxt') && ishandle(handles.valtxt)
set(handles.valtxt, 'String', voltage);
end
if isfield(handles, 'racetext') && ishandle(handles.racetext)
if voltage <= 90
%If condition
else
%Else condition
end
end
end
0 Comments
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!