MATLAB: How to check if matlab window has focus

check focusfinishstartup

Hello, My target is to create a time-object which will check every 10 minutes if Matlab is used. Do you have any ideas how to do it? I know how to create time object and how to use startup.m, finish.m scripts, but how can I verify if Matlab is running (and really in use) or it is just minimalized or covered by another window when user does something else?
Best regards, Pawel

Best Answer

I guess you use Windows
  • download PsList (better than tasklist)
  • the timer callback function shall call pslist.exe
% pslist.exe shall be in the same folder as this function
dos_fmt = [ fullfile( fileparts( which( mfilename ) ), 'pslist.exe' ), ' %s' ];
dos_fmt = strrep( dos_fmt, '\', '\\' );
dos_cmd = sprintf( dos_fmt, num2str(val) ); % num2str takes strings
mlb_cmd = sprintf( 'dos( ''%s'' );', dos_cmd );
dos_buf = evalc( mlb_cmd );
where val is equal to 'matlab' (or it's ProcessID)
Compare the current CPU Time and Elapsed Time with the previous ones.
An alternative is JavaScript in combination with the windows script host, wscript