Matlab - Force to Retain Breakpoints - matlab

I wish to know, if there is a way to force Matlab to retain all historically placed breakpoints -red dots which enable code debugging- in the Matlab Editor/Debugger inside functions, classes, etc. from one session to another, for example, and without being deleted with clear all commands.
It would be easy for debugging huge pieces of software while changes are introduced, and because Matlab sometimes simply shut down because of internal errors.
Thanks fellows.

dbstop is the cleaner solution. Just insert it in the place you want the debugging to stop and this will not be removed until you edit or comment the line.

You need to save the breakpoints and reload them in next session. You can use dbstatus to get a structure that contains information about all breakpoints and save it into a file:
s = dbstatus('-completenames');
save FILENAME s
and later retrieve them using dbstop
load FILENAME
dbstop(s);
You can automate it by including it in startup.m and finish.m files (create them on default user path if they don't exist).

Related

How to replace value in txt file with powershell from GitHub

I want to build a simple script that may be useful for others as well, but I have only very basic programming knowledge and can't do it myself without learning how to write powershell scripts from scratch.
What this script is supposed to do is, open an INI file (really just a txt), look for a variable with an assigned value and replace that value from a txt hosted on GitHub, save and then run a program.
This is for the tracker list of qBittorrent, since that feature still hasn't been implemented and the only other script that I could find that does this is for linux and mac, there seem to be none for windows.
The basic idea is this:
get-content "c:\users\[user]\appdata\roaming\qbittorrent\qbittorrent.ini"
# This is where pseudo code starts
get file from "[github-link.txt]"
save file to cache # keeping it is useless as it gets updated daily
find variable "Session\AdditionalTrackers=" in qbittorrent.ini
replace value of variable with content of cached file # this is what I struggle with most when looking for example code. Everything I could find specified the exact string that needed replacing, which in this case is quite long and may change with every update of the file.
overwrite original file
launch program qbittorrent.exe
end script
Conveniently or most likely deliberately all (most) of the tracker lists on GitHub are already formatted in a way that they can be directly pasted into the file without having to worry about formatting. Example.
I can totally understand if nobody wants to do the work, but I would greatly appreciate it and possibly others that are looking for a stopgap for the lacking feature.
If this already exists, go ahead and call me an idiot and while you're at it drop a link ;)
I just found a little tool called Power Automate and it pretty much does what I was looking for. It's not quite as elegant as a single click script but it does the job. Sadly I can't share the "flow" I built because, well, there is no option for it - thanks Microsoft. So, I'll try my best to write it out.
Not quite a "solution" but pretty to close to it.
Here is the "flow":
get file from web // from github for example
read text from file // read downloaded .txt file
read text from file // read qBittorrent.ini
crop text // crop between flags in qBittorrent.ini use "Session\AdditionalTrackers=" as start and "Session\GlobalMaxRatio=" as end and save to cropVar2
crop text // crop before flag use "Session\AdditionalTrackers=" as flag and save to cropVar1
crop text // crop after flag use cropVar2 as flag and save to cropVar3
replace text // replace cropVar2 with content of downloaded file and save to cropVar2
write text to file // write cropVar1,cropVar2,cropVar3
end flow
Keep in mind that any changes to the qBittorrent.ini may change the order of the entries. Which means you have to check if it's still correct after every update and after every change you make in the options. This is a massive cludge after all...
You can input fail saves so that you won't break anything if the order changed.

How to save a session on Matlab

I was trying to save my Matlab variables and environment into a session so that next time I can directly load them without repeating the codes. I know how to save the Workspace variables but their way of saving a session doesn't work. It's said to be File -> Save to a session. But I couldn't find the button "File".
Any thoughts?
OK Actually I figured out an alternative solution. I realized that I could save all the Workspace variables. But my variables are too huge. So I ended up saving all my commands in a .m script file.
So next time when I want to resume my work, I just type the name of that script file to run all the useful commands to create the working variables and set up the script paths I need.

How to trigger breakpoints inside a script called by publish?

I wrote some script called drawCurves.m to draw some curves. Then I called this script via publish function:
publish('drawCurves.m', 'outputDir', destPath, 'showCode', false)
Unfortunately, no any breakpoints set inside drawCurves trigger. If I call script directly by name, breakpoints do trigger.
Is it possible to fix somehow?
The publish command calls a private function evalmxdom to actually run the code in your file. You can find evalmxdom at $matlabroot%\toolbox\matlab\codetools\private\evalmxdom.m, where $matlabroot$ is your MATLAB installation directory.
If you read through it, you'll find a section at around line 60 where it stores the current breakpoints, turns them all off, and then sets things up so that the original breakpoints are restored when publishing is finished.
You'll notice that the code calls a subfunction safeDbclear that clears the breakpoints. Try commenting out the contents of that subfunction, and publishing again.
NB:
I'm on R2017a: if you're on a different version and this file has changed between versions, the line numbers may be different.
Be careful: you will be modifying files that are part of your MATLAB installation here. Take a backup of the file before you make a change to it.
This may well mess up some aspects of publishing - it's turning off breakpoints for a reason.

Script for running (testing) another matlab script?

I need to create a matlab mfile that will run another matlab file with default values given in a txt file. It's ment to be useful for testing programs, so that user may specify values in a txt files and instead of inputing values every time he starts the program, my script will give the program default values and user will only see the result.
My idea is to load tested file into a variable, change 'variable=input('...');' for variable = default_variable;, save it to tmp file, execute, and than delete tmp file. Is this going to do the job?
I have only two problems:
1) How to eliminate the problem of duplicated variable names - i mean this must work for all scripts, i don't know the names of variables used in tested script.
2) As I wrote before - is this going to work fine? Or maybe I missed a easier way to do it - for example maybe I don't have to create a tmp file?
I really need your help!
Thanks in advance!
If the person who has to edit the default values has access to Matlab, I would recommend saveing the values in a mat file and loading them when needed. Otherwise you could just write a smalls cript that contains the assignment to certain variables, but make sure to keep this small. For example:
maxRuns = 100;
clusters = 12;
So much for setting up the defaults. Regarding the process my main advice is to wrap the thing that you want to test into a function. This way variables used in the code to call the 'script' will not interfere as a function gets its own separate workspace. Check doc function if you are not familiar with them.

Retrieve Variables/Accesing Variables in different Workspaces

I imported some data and them cleaned it up, there is a lot of data so this is quite time consuming.
I then wrote a scipt that uses these variables.
However when I went to run the script, it says that these varibles are not know. In the workspace editor is only shows variables declared in the script.
Q1.) Is there a way to get the original variables back?
Secondly as the clean up operation takes quite some time I don't want to have to import that data and clean in every time I run the M-file.
Q2.) So once I have the cleaned data how do I make these variables available to an M file (do I have to declare them as global or something like that?)
To be clear I tried to just debug the script rather than running it from the command window, I just wnat to get the base variables back now if possible!
ahem there was a errant clear used in the script that's why all the variables were disappearing.
A misplaced Clear or Clear all may be the problem. Similar thing happened to me once.