How to record time and position of a mouse cursor simultaneously and save it in file using "GUIDE"?

1 view (last 30 days)
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);

Accepted Answer

Geoff Hayes
Geoff Hayes on 16 Feb 2022
@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 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps 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!