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
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 件のコメント

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by