Setting up a function to tag scripts for notifications

4 views (last 30 days)
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?

Accepted Answer

Dinesh
Dinesh on 1 Feb 2024
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 Comment
Nedim Gökhan Aydin
Nedim Gökhan Aydin on 1 Feb 2024
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings 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!