How to have an event when the mouse enters a particular area/polygon in Guide?

3 Ansichten (letzte 30 Tage)
I would like an event whenever a mouse enters a particular area. Basically, i would like motors to be running when the mouse enters the area.Also i would like the area to turns green when the cursor is inside the area and turns red when it is out.
I had the code in appdesigner but appdesigner is paradoxically limited. I dont really know how to do it in Guide. *****Check the Next_buton callback
Furthermore, with the help of a "Next button", i would like data to be loaded from excel (data are different polygones)
function varargout = Hapticfinal(varargin)
% HAPTICFINAL MATLAB code for Hapticfinal.fig
% HAPTICFINAL, by itself, creates a new HAPTICFINAL or raises the existing
% singleton*.
%
% H = HAPTICFINAL returns the handle to a new HAPTICFINAL or the handle to
% the existing singleton*.
%
% HAPTICFINAL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HAPTICFINAL.M with the given input arguments.
%
% HAPTICFINAL('Property','Value',...) creates a new HAPTICFINAL or raises the
% existing singleton*. Starting from the left, property value pairs are
% handleslied to the GUI before Hapticfinal_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property handleslication
% stop. All inputs are passed to Hapticfinal_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 Hapticfinal
% Last Modified by GUIDE v2.5 17-Feb-2022 21:51:18
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Hapticfinal_OpeningFcn, ...
'gui_OutputFcn', @Hapticfinal_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 Hapticfinal is made visible.
function Hapticfinal_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 Hapticfinal (see VARARGIN)
% Choose default command line output for Hapticfinal
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Hapticfinal wait for user response (see UIRESUME)
% uiwait(handles.finger);
% --- Outputs from this function are returned to the command line.
function varargout = Hapticfinal_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 mouse motion over figure - except title and menu.
function finger_WindowButtonMotionFcn(hObject,eventdata, handles)
% hObject handle to finger (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pos = get(hObject, 'currentpoint'); % get mouse location on figure
global x;
global y;
x = pos(1);
y = pos(2); % assign locations to x and y
set(handles.xloc, 'string', ['x loc:' num2str(x)]); % update text for x loc
set(handles.yloc, 'string', ['y loc:' num2str(y)]); % update text for y loc
% --- Executes on button press in Start_button.
%function Start_button_Callback(hObject, eventdata, handles)
% --- Executes on button press in Start_button.
function Start_button_Callback(hObject, eventdata, handles)
% hObject handle to Start_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%clear
%clc
handles.fileID = fopen('exp.txt','w');
handles.t = timer('ExecutionMode', 'fixedRate', ...
'Period', 0.5, ...
'TasksToExecute', Inf, ...
'TimerFcn', {@timerCallback, handles.fileID});
start(handles.t);
set(handles.Start_button,'Enable','off'); % -> Disable the button
guidata(hObject,handles);% -----> do this to save the updated handles object
function timerCallback(~,~,fileID)
fprintf(fileID,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
%fprintf('calling timer callback\n');
% --- Executes on button press in Stop_button.
function Stop_button_Callback(hObject, eventdata, handles)
% hObject handle to Stop_button (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);
stop(handles.t) %whenever we want to stop.
fclose(handles.fileID);
set(handles.Start_button,'Enable','on'); % -> Enable the button
guidata(hObject,handles);
% --- Executes on button press in Yes_button.
function Yes_button_Callback(hObject, eventdata, handles)
% hObject handle to Yes_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
answer = questdlg('ARE YOU SURE?','Confirm close',...
'OK',@(src,event)mycallback(handles,src,event));
clear
clc
fileID= fopen('exp.txt2','a');
YES = "I FEEL IT";
fprintf (fileID, "%s\n",YES);
fclose(fileID);
% --- Executes on button press in NO_button.
function NO_button_Callback(hObject, eventdata, handles)
% hObject handle to NO_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
answer1= questdlg('ARE YOU SURE?','Confirm Close',...
'OK',@(src,event)mycallback(handles,src,event));
clear
clc
fileID= fopen('exp.txt2','a');
NO = "I DON'T FEEL IT";
fprintf (fileID, "%s\n",NO);
fclose(fileID);
% --- Executes on button press in Next_button.
function Next_button_Callback(hObject, eventdata, handles)
% hObject handle to Next_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)uiconfirm(handles.UIFigure,'Are You sure?','Confirm Close',...
f = msgbox('Operation Completed','NEXT');
% Read experiment data from a CSV file
% region1 = patch(axes1,[-10 10 10 -10],[-5 -5 -4.4 -4.4],'r','FaceAlpha',1,...
%'LineWidth',0.01,'LineStyle','-','tag','region1');
load_folder = "C:\Users\Hp\Desktop\thesis\excel_data\";
load_name = "excel_data.xlsx";
load_addr = load_folder + load_name;
T = readtable(load_addr,'NumHeaderLines',1);
exp_counter = 1;
v_thickness_1 = T.Var1;
v_thickness_2 = T.Var2;
h_thickness_1 = T.Var3;
h_thickness_2 = T.Var4;
amplitude_array = T.Var5;
v_or_h_array = T.Var6;
v_thick1 = v_thickness_1(exp_counter);
v_thick2 = v_thickness_2(exp_counter);
h_thick1 = h_thickness_1(exp_counter);
h_thick2 = h_thickness_2(exp_counter);
%%
v_or_h = v_or_h_array(exp_counter);
%Vertical line
if v_or_h == 0
patch([v_thick1 v_thick2 v_thick2 v_thick1],[-10 -10 10 10],'red');
xlim([-5 0])
grid on
%Horizontal line
elseif v_or_h == 1
patch([-10 10 10 -10],[h_thick1 h_thick1 h_thick2 h_thick2],'red');
ylim([0 5])
grid on
end
% % Create the Arduino serial object
% %arduinoObj = serialport("COM3", 9600);
% %configureTerminator(arduinoObj,"CR/LF");
% %flush(arduinoObj);
% %
% for i=1:8
% message = readline(arduinoObj);
% disp(message)
% end
global Start;
Start=1;
if v_or_h == 0
patch([v_thick1 v_thick2 v_thick2 v_thick1],[-10 -10 10 10],'red');
xlim([-5 0])
grid on
%Horizontal line
elseif v_or_h == 1
patch([-10 10 10 -10],[h_thick1 h_thick1 h_thick2 h_thick2],'red');
ylim([0 5])
grid on
end
% Create the Arduino serial object
arduinoObj = serialport("COM3", 9600);
configureTerminator(app.arduinoObj,"CR/LF");
%flush(app.arduinoObj);
%
for i=1:8
message = readline(arduinoObj);
disp(message)
end
function cursorPositionFeedback(app, hobj, inout) %This is a code from appdesigner
% When inout is 'in', change hobj facecolor to green and update textbox.
% When inout is 'out' change hobj facecolor to red, and clear textbox.
% Check tag property of hobj to identify the object.
switch lower(inout)
case 'in'
% facecolor = 'g';
% txt = 'Motor(s) vibrating';
pointer = 'fleur';
writeline(app.arduinoObj, "4&MOTOR_1_2&0!"); % motors running
% message = readline(app.arduinoObj);
% disp(message)
case 'out'
% facecolor = 'r';
%txt = 'No';
pointer = 'crosshair';
writeline(app.arduinoObj, "0&MOTOR_1_2_3_4&0!");
drawnow;
% message = readline(app.arduinoObj);
% disp(message)
end
hobj.FaceColor = facecolor;
app.TextAreaEditField.Value = txt;
set(app.UIFigure, 'Pointer', pointer)
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function finger_CreateFcn(hObject, eventdata, handles)
% hObject handle to finger (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
  19 Kommentare
Franck paulin Ludovig pehn Mayo
Bearbeitet: Franck paulin Ludovig pehn Mayo am 23 Feb. 2022
@_ sorry for disturbing you again....
I would like to edit the code with this. But the code is in appdesigner,i dont know how to implement this in guide.
function cursorPositionFeedback(app, hobj, inout)
% Responds to cursor entering/exiting vertical band
persistent timein
switch inout
case 'in'
hobj.FaceColor = 'g';
timein = datetime('now');
case 'out'
hobj.FaceColor = 'c';
timeInPatch = seconds(datetime('now')-timein);
ax = ancestor(hobj, 'axes');
cp = ax.CurrentPoint;
text(ax, 0.52, cp(1,2), sprintf('%.3f sec.', timeInPatch), ...
'HorizontalAlignment','Left', ...
'VerticalAlignment','bottom', ...
'FontSize', 12, ...
'FontWeight', 'bold', ...
'Color', 'g')
end
end
function WindowButtonMotion(uifig, event, ax)
% Responds to cursor moving in figure window.
cp = ax.CurrentPoint;
isInAxes = cp(1,1) >= ax.XLim(1) && ...
cp(1,1) <= ax.XLim(2) && ...
cp(1,2) >= ax.YLim(1) && ...
cp(1,2) <= ax.YLim(2);
if isInAxes
plot(ax, cp(1,1), cp(1,2), 'k.')
end
end
Voss
Voss am 23 Feb. 2022
That code looks like it is using an iptPointerManager, which is not something I have worked with before. It may be possible to still use an iptPointerManager in your existing GUIDE code, but I don't think it's really necessary.
It looks like that function cursorPositionFeedback creates a text when the cursor exits the patch region that shows the amount of time the cursor was over the patch. You can achieve the same thing with something like the following.
First, in your OpeningFcn, create the text object and additional variables that say whether the cursor is currently on the patch and when the cursor last entered the patch region:
handles.time_text = text(handles.axes1);
handles.on_patch_region = false;
handles.time_entered_patch_region = NaN;
Then in finger_WindowButtonMotionFcn, you could have some logic like this (which replaces the block currently at the bottom of that function, where the patch's FaceColor is set):
if x >= p_x(1) && x <= p_x(2) && y >= p_y(1) && y <= p_y(2)
set(handles.region1,'FaceColor','g');
if ~handles.on_patch_region
handles.on_patch_region = true;
handles.time_entered_patch_region = datetime('now');
end
else
set(handles.region1,'FaceColor','r');
if handles.on_patch_region
handles.on_patch_region = false;
set(handles.time_text, ...
'String',sprintf('%.3f sec.', seconds(datetime('now')-handles.time_entered_patch_region)), ...
'Position',[0 0]); % update the Position too if you want to
end
end
% now it is necessary to store the updated handles structure
% because handles.on_patch_region, etc., might have changed
guidata(hObject,handles);

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cris LaPierre
Cris LaPierre am 19 Feb. 2022
Bearbeitet: Cris LaPierre am 20 Feb. 2022
See the demo towards the bottom of this page.

Kategorien

Mehr zu Specifying Target for Graphics Output 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