Any idea on why a particular line of code is not read even in the debugging mode?

2 Ansichten (letzte 30 Tage)
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);

Akzeptierte Antwort

Voss
Voss am 2 Jul. 2022
Checking whether x and y are each within some constant limits, like you have:
if x >= p_x(1) && x <= p_x(2) && y >= p_y(1) && y <= p_y(2)
is equivalent to checking whether the point (x,y) is within a rectangular region.
But in this case the patch is not a rectangle:
handles.axes1 = gca();
handles.region1 = patch( ...
'Parent',handles.axes1, ...
'XData',[5 5 20 10], ...
'YData',[0 10 15 0], ...
'FaceColor','red');
So you'd have to use inpolygon or equivalent to determine whether the cursor is on the patch.
Also, the rectangular region you're actually checking has zero width, because p_x(1) == p_x(2):
p_x = get(handles.region1,'XData');
p_x = p_x([1 2])
p_x = 2×1
5 5
And it extends beyond the patch in the y direction:
p_y = get(handles.region1,'YData');
p_y = p_y([1 3])
p_y = 2×1
0 15
In other words, the region you're checking is a line along the left edge of the patch that extends above the patch:
% new figure with same patch, for demonstration purposes:
figure();
handles.axes1 = gca();
handles.region1 = patch( ...
'Parent',handles.axes1, ...
'XData',[5 5 20 10], ...
'YData',[0 10 15 0], ...
'FaceColor','red');
% a line showing p_x and p_y
line(p_x,p_y,'Color','y','LineWidth',3)
So the patch will turn green only when the cursor is exactly on that yellow line (which may never actually happen, in fact).
  4 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by