Any idea on why a particular line of code is not read even in the debugging mode?
Show older comments
I am trying to run a code. I would like that each time the pointer enters a region , it turns green..But i have been uncessful. It is the last "paragraph" of the code.
The line in question is
"set(handles.region1,'FaceColor','g'); "
function varargout = hapticpen(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @hapticpen_OpeningFcn, ...
'gui_OutputFcn', @hapticpen_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 hapticpen is made visible.
function hapticpen_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 hapticpen (see VARARGIN)
% Choose default command line output for hapticpen
handles.output = hObject;
%
handles.region1 = [];
% % Create the Arduino serial object
% handles.arduinoObj = serialport('COM1', 38400);
% configureTerminator(handles.arduinoObj,'CR/LF');
% %
% for i=1:8
% handles.message = readline(handles.arduinoObj);
% disp(handles.message)
% end
% create_patch(handles);
% Update handles structure
guidata(hObject, handles);
% function create_patch(handles)
% if ishandle(handles.region1)
% delete(handles.region1);
% end
handles.region1 = patch( ...
'Parent',handles.axes1, ...
'XData',[5 5 20 10], ...
'YData',[0 10 15 0], ...
'FaceColor','red');
set(handles.axes1,'XLim',[-10 10],'YLim',[0 10]);
%set(handles.axes1,'XLim',[0 5],'YLim',[0 10]);
set(handles.axes1,'XGrid','on','YGrid','on');
axis(handles.axes1,'equal');
% Update handles structure
%guidata(handles.fig_hapticpen,handles);
guidata(hObject, handles);
% call the button motion fcn to update the new patch's color:
fig_hapticpen_WindowButtonMotionFcn(handles.fig_hapticpen);
% UIWAIT makes hapticpen wait for user response (see UIRESUME)
% uiwait(handles.fig_hapticpen);
% --- Outputs from this function are returned to the command line.
function varargout = hapticpen_OutputFcn(~, 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 mouse motion over figure - except title and menu.
function fig_hapticpen_WindowButtonMotionFcn(hObject, ~, handles)
% hObject handle to fig_hapticpen (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
pos = get (hObject,'currentpoint');
global x;
global y;
x = pos(1);
y = pos(2);% assign location to x and y
set(handles.lbl_x,'string',['x loc:' num2str(x)]); %update text for x loc
set(handles.lbl_y,'string',['y loc:' num2str(y)]); %update text for x loc
% Determine if mouse is within the region
p_x = get(handles.region1,'XData');
p_x = p_x([1 2]);
p_y = get(handles.region1,'YData');
p_y = p_y([1 3]);
ax_xl = get(handles.axes1,'XLim');
ax_yl = get(handles.axes1,'YLim');
ax_units = get(handles.axes1,'Units');
if ~strcmp(ax_units,'pixels')
set(handles.axes1,'Units','pixels')
end
ax_pos = get(handles.axes1,'Position'); % axes1 position in pixels
if ~strcmp(ax_units,'pixels')
set(handles.axes1,'Units',ax_units);
end
% convert the patch XData and YData from axes coordinates to figure coordinates in pixels
p_x = (p_x-ax_xl(1))/(ax_xl(2)-ax_xl(1))*ax_pos(3)+ax_pos(1);
p_y = (p_y-ax_yl(1))/(ax_yl(2)-ax_yl(1))*ax_pos(4)+ax_pos(2);
if x >= p_x(1) && x <= p_x(2) && y >= p_y(1) && y <= p_y(2)
set(handles.region1,'FaceColor','g');
% writeline(handles.arduinoObj, '1&1!')
else
set(handles.region1,'FaceColor','r');
%writeline(handles.arduinoObj, '0&1!')
end
guidata(hObject, handles);
2 Comments
Walter Roberson
on 2 Jul 2022
You might need drawnow()
Franck paulin Ludovig pehn Mayo
on 2 Jul 2022
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Objects 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!
