Setting up a function to tag scripts for notifications

조회 수: 6 (최근 30일)
Nedim Gökhan Aydin
Nedim Gökhan Aydin 2022년 3월 31일
댓글: Nedim Gökhan Aydin 2024년 2월 1일
I have a simple notification setup that is using pbNotify from FileExchange, which sends a notification to my mobile phone via Pushbullet when a long-running script finishes. For the setup to work, I have to add a line with my token and another line containing my message. I also generally pass the elapsed time with tic-toc command couple to the message (it is really convenient!).
Now, I do not want to include the two lines of code in my scripts and comment/uncomment occasionally. I want to prepare a function that does this work "semi-automatically".
Say that I have a script called "work.m", running time of which depends on the amount of data I input. Whenever I know that the script will take a long time to process the data, I will come to the Command Window and call my script, say that it is "add_notification.m". When I call add_notification.m , the work.m script's notifications will be turned on and when I run work.m, I will always get notifications upon completion until I toggle it off. Note that I do not add anything on work.m.
I have some ideas for this to work. For example, I can get an active (in editor) script's path with:
matlab.desktop.editor.getActiveFilename
I can use this with add_notification.m to prepare some kind of a "name pool", including the names of the scripts that I want notifications from. Then, I need to find a way to check if work.m has completed processing without adding anything into it manually. If work.m has completed, then another script, say it is called "notificate.m" runs automatically and checks if the completed script's name is included in the name pool. If the name is in the list, then I get a notification, if not, nothing happens.
Do you have any ideas? Is this possible?
I can also simplify my question into these sub-questions:
  • Is it possible to "programmatically" check if a script have completed running?
  • ...if it is possible, then how can I run another script right after to get notifications?

채택된 답변

Dinesh
Dinesh 2024년 2월 1일
Hi Nedim,
To achieve your goal of receiving notifications without modifying your original script, you can wrap the execution of "work.m" within another function, "add_notification.m", which will execute "work.m" and upon completion, check against a list of scripts set for notification and trigger "notificate.m" if there’s a match. You can use MATLAB's "evalin" function to run "work.m" within the base workspace from "add_notification.m", and then use "onCleanup" to ensure "notificate.m" is called after "work.m" completes.
The following links might help you:
  댓글 수: 1
Nedim Gökhan Aydin
Nedim Gökhan Aydin 2024년 2월 1일
Hi Dinesh,
Thank you very much for your answer. It seem I need to create another script regardless, yet using "onCleanup" definitely the solution I've been looking for. Thanks a lot!
I had given up the case and have just been using a command window function I have created, notifying me about the conclusion of the long-running script. Still, finding a solution makes me happy.
What comes to my is to create a "controller" function with three inputs in its most basic form: the main (work) function I want to run, the function I want it to run after the first one finishes (using onCleanup) and send the notification with a specific message. Something like:
controller_func(@dothiswork, @thendothis, "show me this message")
Again, thank you very much for the answer! Checked it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by