How to record time and position of a mouse cursor simultaneously and save it in file using "GUIDE"?
1 次查看(过去 30 天)
显示 更早的评论
I am trying to record time and position each time the mouse is moving on the canvas in Guide and save it in file....
In appdesigner, it's something like the link below.(I know Guide is going to be deleted in the future release but appdesigner is just short on what i am doing). This code is not working on Guide, any help please?
clear
clc
fileID = fopen('exp.txt','w');
t = timer('ExecutionMode', 'fixedRate', ...
'Period', 0.05, ...
'TasksToExecute', Inf, ...
'TimerFcn', @(~,~) fprintf(fileID,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now') ));
start(t);
%stop(t) %whenever we want to stop.
%fclose(fileID);
采纳的回答
Geoff Hayes
2022-2-16
@Franck paulin Ludovig pehn Mayo if you need to use GUIDE, you could do the following in whatever callback that would start the recording (I suspect the issue is that the fileID is unknown when the code tries to write to file)
handles.fileID = fopen('exp.txt','w');
handles.t = timer('ExecutionMode', 'fixedRate', ...
'Period', 0.5, ...
'TasksToExecute', Inf, ...
'TimerFcn', {@timerCallback, handles.fileID});
start(handles.t);
guidata(hObject,handles); % ----> do this to save the updated handles object
Your callback would then look like
function timerCallback(~,~,fileID)
fprintf(fileID,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
In whatever callback (or exit) function that you wish to stop the timer and close the file, then you would do the following (here I'm assuming in the exit function)
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
stop(handles.t) %whenever we want to stop.
fclose(handles.fileID);
% Hint: delete(hObject) closes the figure
delete(hObject);
20 个评论
Franck paulin Ludovig pehn Mayo
2022-2-16
@Geoff Hayes the problem was a " ; " issue. Actually, the code i posted is working but i have a problem with closing the file ID. The mistake i am having now is "Unrecognized function or variable 't'."
I have added guidata(hObject,handles) at the end of the coded i posted first but it seems it does not have any impact on the one below.
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)
guidata(hObject);
stop(t);
close(fileID);
guidata(hObject,handles);
Geoff Hayes
2022-2-16
@Franck paulin Ludovig pehn Mayo you will need to save the t and fileID to the handles structure (as I've shown above) and then use that object/structure to stop the timer and close the file (again like in the figure1_CloseRequestFcn code). Please note that calling
guidata(hObject);
and
guidata(hObject,handles);
in your Stop button callback doesn't really do anything. The first line of code would return an object (which you are not capturing) and the second line of code is only necessary if you have made a change to the handles object.
Franck paulin Ludovig pehn Mayo
2022-2-16
@Geoff Hayes I understood the whole concept. Howerver, i am a little bit confused... I think i made a mistake somehwere
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});
function timerCallback(~,~,fileID)
fprintf(fileID,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
start(handles.t);
Geoff Hayes
2022-2-17
@Franck paulin Ludovig pehn Mayo - in your above code, you have placed the timer callback with the start button callback (or, I suppose, some of the code from the start callback into the timer callback). Separate them as
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);
guidata(hObject, handles); % <--- you need this line too!!
and
function timerCallback(~,~,fileID)
fprintf(fileID,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
Note also the inclusion of the guidata line of code.
Franck paulin Ludovig pehn Mayo
2022-2-17
@Geoff HayesStill confusing a lile bit.I am using Guide not a programmically GUI. This is the whole code that i am adjusting step by step.Where should i put the code?
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 16-Feb-2022 21:09:53
% 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)
% 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});
fprintf(handles,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
start(handles.t);
fprintf(handles,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
guidata(hObject,handles); % ----> do this to save the updated handles object
% --- 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)
stop(handles.t) %whenever we want to stop.
fclose(handles.fileID);
% --- 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',...
'CloseFcn',@(src,event)mycallback(handles,src,event));
clear
clc
fileID= fopen('exp.txt2','w');
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',...
'CloseFcn',@(src,event)mycallback(handles,src,event));
clear
clc
fileID= fopen('exp.txt2','w');
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',...
% --- 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)
Geoff Hayes
2022-2-17
@Franck paulin Ludovig pehn Mayo - the code I posted is from a GUIDE example. Please change your Start_button_Callback to
% --- 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);
guidata(hObject,handles); % ----> do this to save the updated handles object
and then (if you want) at the bottom of this file, add the following function
function timerCallback(~,~,fileID)
fprintf(fileID,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
and that should work.
Franck paulin Ludovig pehn Mayo
2022-2-17
@Geoff Hayes Thank you bu now i am having errors with the stop button.... The error is :
Unrecognized field name "t".
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)
stop(handles.t) %whenever we want to stop.
fclose(handles.fileID);
Geoff Hayes
2022-2-17
@Franck paulin Ludovig pehn Mayo - so long as your start button callback has the (final) line as
guidata(hObject,handles); % ----> do this to save the updated handles object
then the t and the fileID should be saved to the handles structure. I would put a breakpoint at the
stop(handles.t) %whenever we want to stop.
and then run your code. When you press the STOP button, the debugger will pause at this line and you should be able to see what fields are in handles.
Franck paulin Ludovig pehn Mayo
2022-2-17
@Geoff Hayes "t" is not in the field....I even try with define "t" as global but it still not working
Geoff Hayes
2022-2-17
@Franck paulin Ludovig pehn Mayo can you confirm that this line of code
guidata(hObject,handles); % ----> do this to save the updated handles object
is the last line of your start button callback?
Geoff Hayes
2022-2-17
@Franck paulin Ludovig pehn Mayo - I think that you will need to post your code and the fig file so that I can try and debug the issue.
Franck paulin Ludovig pehn Mayo
2022-2-17
@Geoff Hayes Attached is the code.
Start and stop callbacks
Geoff Hayes
2022-2-18
编辑:Geoff Hayes
2022-2-18
@Franck paulin Ludovig pehn Mayo Please remove the
clear
clc
from your GUI code (in the Start button callback). This should resolve the issue. Was there a reason why you were doing this?
Franck paulin Ludovig pehn Mayo
2022-2-18
编辑:Franck paulin Ludovig pehn Mayo
2022-2-18
@Geoff Hayes Thank you. The error stops but the Stop button is not playing its role. I would like that when i click on the stop_button, everything stop recording.
Also, is there any function that can add inside that will record in term of second (Instead of date "now").
Well, i have many users that will use the Gui, i thought i could have done programmatically instead of deleting the outputs directly from the file each time.
Geoff Hayes
2022-2-18
@Franck paulin Ludovig pehn Mayo so when you say the "stop button is not playing its role", what exactly do you mean? Are there errors in the console when you press the stop button that might indicate what is going wrong? Do you perhaps have more than one timer running? To ensure that the user cannot create a new timer, I think that you will want to disable the start button once they press it. You can re-enable this button once the stop button is pressed. Something like
% --- 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)
handles.fileID = fopen('exp.txt','w');
handles.t = timer('ExecutionMode', 'fixedRate', ...
'Period', 0.5, ...
'TasksToExecute', Inf, ...
'TimerFcn', {@timerCallback, handles.fileID});
start(handles.t);
set(hObject,'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)
stop(handles.t) %whenever we want to stop.
fclose(handles.fileID);
set(hObject,'Enable','on'); % -> Enable the button
Franck paulin Ludovig pehn Mayo
2022-2-18
@Geoff Hayes No errors in the command window. I meant by that even though i click on the stop button , the gui is still recorded time and position in the file . I have a little bit modified the code you suggested cos i was having some errors .
I just have one timer in the code and that is in the start_button.
The question might be how do i check whether the stop button is working or not?
It is simple , i just look the time and for like 10sec i keep hovering my mouse on the canvas , then click on stop waiting for the next minute and mouve the mouse to see if it will be recorded.
To be clearer, i can start hovering the mouse at 7:21 mn and then click on stop. When the time goes to 7:22 will do the same process and then check if 7:22 was recorded or not.
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);
Geoff Hayes
2022-2-18
@Franck paulin Ludovig pehn Mayo - so does disabling the button help or not? When I ran the code (that I provided), I could see that when I pressed the stop button, there were no more messages written to the console like calling timer callback (the line you have commented out in the above).
From To be clearer, i can start hovering the mouse at 7:21 mn and then click on stop. When the time goes to 7:22 will do the same process and then check if 7:22 was recorded or not., what is the "same process"?
From The question might be how do i check whether the stop button is working or not? well you can check to see if the file is still being updated. You seem to suggest that it is but I'm not sure about that...that is why I left in the line
fprintf('calling timer callback\n');
so that if the timer were still running, then this line would be written to the console. Do you see this line still running with the above code after you have pressed the stop button?
Franck paulin Ludovig pehn Mayo
2022-2-18
@Geoff Hayes After pressing the stop button, the 'calling timer callback' stop from running.
Please Would you mind having a look on the following link?
Franck paulin Ludovig pehn Mayo
2022-2-22
@Geoff Hayes please would you mind having a look on this : https://in.mathworks.com/matlabcentral/answers/1655830-running-motors-when-mouse-cursor-enters-a-region
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)