is there a way to update workspace or variable editor values during running script without a break point - matlab

I would like to update variable editor at a predefined interval during a running script without having to use a break. Is this possible? I have a sim that runs for hours, I would like to be able to come back every hour and grab some values off a matrix in variable editor to play with in excel without interrupting running script. Any ideas would be greatly appreciated.

I think using assignin to copy your matrix to the base workspace should do exactly what you want. You'd need to manually reopen the variable in the editor to reflect the new data if it's changed.
If you wanted to get fancier, I imagine you could script evalin and openvar to do it for you, but I no longer have real Matlab to test with and Octave's fledgling GUI isn't there yet.

Related

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.

Matlab - Force to Retain Breakpoints

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).

Transform current workspace variables into code

Sometimes I would rather not like to show my data to other people and therefore I need to modify it. When it is ready for export, I wish to export the workspace data, for instance a table, exactly as it is, for POSTING it here for instance.
I know the generate script function, and I feel the answer is somewhat related but it is not that, and publish is also not the command I require. How do I do that?
UPDATE with example:
How do I print a table as code?
%% Table example to export workspace
T={'A2P3';'A2P3';'A2P3';'A2P3 with (extra1)';'A2P3 with (extra1) and (extra 2)';'A2P3 with (extra1)';'B2P3';'B2P3';'B2P3';'B2P3 with (extra 1)';'A2P3'};
a={1 1 0 1 1 0 1 1 0 1 1 }
T(:,2)=num2cell(1);
T(3,2)=num2cell(0);
T(6,2)=num2cell(0);
T(9,2)=num2cell(0);
T=table(T(:,1),T(:,2));
class(T.Var1);
class(T.Var2);
T.Var1=categorical(T.Var1)
T.Var2=cell2mat(T.Var2)
class(T.Var1);
class(T.Var2);
Since Matlab 2014a there is this new feauture:
MATLAB now provides the ability to save workspace variables to a
MATLAB script. Once the script is saved, you can regenerate the
workspace variables by running the script. Click Save Workspace on the
MATLAB desktop and select MATLAB script (*.m) in the Save as type
menu. Alternately, use matlab.io.saveVariablesToScript to perform this
operation from the command line.
Documentation fo matlab.io.saveVariablesToScript
If you don't have the new version, you will need to write your own I/O routine for your variables. It's quite easy to do that generic for numerical arrays, but it depends on the certain case.
If Matlab 2014a is not available, I would use gencode from here

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.