Default "legend" function overwritten by accident - matlab

I am using the BNT-toolbox, a big library written in matlab for inference in bayesian networks.
I had to add this toolbox to the path of MATLAB. But after doing that I can't use the default legend function any more.
I think that this library might have his own legend function, overwriting the default one. How can I manually tell MATLAB that I want the original one and not the one in the new toolbox?
Tried in Matlab 2018b and 2020a
EDIT: to reproduce it:
When I run the testscript, it shows the lines and the legend.
https://github.com/bayesnet/bnt, this is the toolbox I talked about. I downloaded it, unzipped and then added it to my path with Home -> Set path -> add folder with subfolder
When I run the script now, it shows the lines and not the legend.
NOTE: when I tried another way of plotting (see testscript 2), the legend shows itself again. So this is a working "workaround"
Testscript1: (location: C:\Users\TomDe\Downloads\FullBNT-1.0.7\bnt\own\testscript1.m)
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
legend('cos(x)','cos(2x)')
Testscript2
% Some other code
tiledlayout(2,1)
nexttile
plot(inputPath)
hold on
plot(sensorPath)
plot(inputInference)
hold off
title('The Input sequence and sensor readings ')
legend('Path', 'sensor', 'Inference')

You can check that that is indeed the case with the which function:
>> which legend -all
It's generally a bad idea to overshadow MATLAB's own functions. I highly suggest you avoid this problem in the first place. Create a MATLAB package and place the source code of this toolbox in there.
For demonstration purposes only, I'll show how to call the real legend.m:
>> wd = pwd;
>> cd 'C:\Program Files\MATLAB\R2020a\toolbox\matlab\scribe\'
>> legend(...)
>> cd(wd);
this being the location of the file on a MATLAB R2020a install.

There are two things you can do:
You always want to use the default legend, never the one in the toolbox: use the -end option to your addpath call when adding the BNT toolbox directory, so that its functions appear at the end of the path. MATLAB will always find functions by looking through the path directories in turn, the directories earlier in the path therefore have precedence.
You want to use both versions of legend, and want to choose which one to use: write a little support function that removes the BTN toolbox from your path, calls legend, then adds the toolbox back in. Such a function looks like this (save it as original_legend.m somewhere in your path, then use it in the same way you'd call legend but using this new name instead):
function out = original_legend(varargin)
rmpath /path/to/bnt/toolbox
out = legend(varargin{:});
addpath /path/to/bnt/toolbox

Related

Programmatically open a Simulink MATLAB function block's code

Can I open a local Simulink MATLAB function block's code in the MATLAB editor via some command?
For example, let us say I have a Simulink model named mainModel.slx.
In it, there is a MATLAB function block named localFunction.
This is not defined in a .m-file.
I would be able to edit the function which path is mainModel/localFunction, without having to open the simulink window and double click on the function block. Is this possible?
I have of course already tried open mainModel/localFunction and edit mainModel/localFunction. I have access to the handle for its StateFlow.EMChart object.
EDIT: Minimal, (hopefully) Complete and Verifiable Example
My minimal Simulink model is shown in the picture below. Code is present below it. For readability, I have not addressed bugs or glitches. It is not for general usage.
The function code for the MATLAB function block localFunction is
function y = fcn(u)
y = 'findThis'; % I want to end up here, in the MATLAB editor!
end
I am using the following code to load the model, search through all MATLAB function blocks and find the ones containing the string 'findThis'. The MATLAB function block named 'localFunction' should then be found. Again, ignore the bugs. The code is saved in a script called tmpScript.m.
% User set
model = 'mainModel';
expression = 'findThis';
blockType = 'Stateflow.EMChart'; % MATLAB function block, right?
% Load model
load_system(model)
% Find all MATLAB function block handles
blockHandles = find(slroot, '-isa', blockType);
% Find first block containing the defined expression
for iHandle = 1:numel(blockHandles)
tmpFind = strfind(blockHandles(iHandle).Script, expression);
if ~isempty(tmpFind)
break
end
end
foundBlockPath = blockHandles(iHandle ).Path; % Function block path
foundCharIdx = tmpFind; % Character index
% Print results in command window
fprintf('Function path: %s\n', foundBlockPath)
fprintf('Character index: %d\n', foundCharIdx)
In this example, the path should be mainModel/localFunction and the character index 29 (Note the three leading white spaces on the function's second line, and that the line break '\n' is worth one characters). The command window shows
>> tmpScript
Function path: mainModel/localFunction
Character index: 29
>>
I can thus load models and search through their MATLAB function blocks for specific strings. When I have found this function, I would like to be able to open it in the matlab editor. What is called when I double click on the block in the Simulink window?
These do NOT work
open(foundBlockPath)
edit(foundBlockPath)
blockHandles(iHandle).openEditor
I cannot change the Simulink model itself. I do not want to change the function script. I just want to be able to open it in the MATLAB editor.
You can open the code in the Editor using,
view(blockHandles(iHandle))
You could change the Matlab function block to an Interpreted Matlab function block.
This does have the limitation that it only can have one input and one output (which can be vectors), so depending on your problem, you might have to mux/demux some data.
Alternatively you can change to an S-function, which gives more flexibility, but might be a bit more complex to setup.

What are the relationships between workspace, .m scripts and Simulink models and how can I use them efficiently?

I'm using a lot of MATLAB for a Control class and something is really bumming me out. They want us to use a lot of Simulink even though I find the visual representation not that helpful and the interface between Simulink and MATLAB scripts hard to figure out in general.
So I have a model and I have added scopes for sinks which can save data directly to the workspace when ran from Simulink. However when I use the command sim in the script to directly use the model according to some parameters (stopTime, solving method, etc.) the results are buried in an object which is poorly documented to say the least so say I have:
simout = sim('lab','StopTime','100','Solver','ode1','FixedStep','2');
Now I have an object in my workspace but to access the data I want I need to go 3, hell 4 layers deep sometimes in the members of simout. My first question is :
Is there a way to access directly or at least know what those members are without using the tedious use of who.
I don't want to compile code to access its documentation! And help is not really helpful for those situations.
Why doesn't the Simulink model save the data when invoked like prescribed in the sink properties. I know that the line of code I mentioned overides some of the simulink block prescriptions (e.g. the solving method used).
How to know how simulink models interact with matlab scripts, granted I am a noob in coding in general, but the documentation doesn't really tell me what are the formal definitions of the model and the way it is used in matlab. I am scared at some points the default settings of 'sim' will overide some settings I set up in an earlier model which would prove to be a nasty buisiness to debug.
TL;DR Is there a quick way to access deeply buried members of an object? For example right now I have to do:
simout = sim('lab','StopTime','100','Solver','ode1','FixedStep','2');
who(simout)
ScopeData = (simout.get('ScopeData'))
signals = (ScopeData.signals)
time = (ScopeData.time)
Can I do something more C-ish of the sort of (Simout->ScopeData).signals?
And finally, why is the MATLAB suite presented like it was an app for day-traders when it is used a lot by EE people who in general need to know their coding? Why are libraries with headers and good documentation for what you are importing in your code (e.g. boost, string etc.) not used? This last option might be less pretty by hiding the mechanics, but to be able to write code properly I feel like I have to know most of the underlying mechanics of the code.
Invariably when most people start off using MATLAB or Simulink, they hate it. The main reasons I see for this is people are taught MATLAB very poorly which prevents them from understanding the power of MATLAB and when it should be used.
Before I get into describing how the MATLAB workspace, m files and Simulink are all related let's first define what each of those is separately and a few of the things you can do with them.
The MATLAB Workspace
The MATLAB workspace contains all of the variables you have created in MATLAB either explicitly via the command window or implicitly through running a .m file (don't worry I'll come back to these). The simplest method to add a variable to the MATLAB workspace is to directly type into the command window like
>> A = 1
A =
1
which adds the variable A to the workspace and assigns it a value of 1. Even simpler though is to simple type 1 like
>> 1
ans =
1
which adds the variable ans to the workspace with a value of 1. The default workspace variable for any command executed by MATLAB that is not explicitly assigned to a variable is ans.
The workspace browser in MATLAB displays information about all of the variables currently in the workspace. For instance if we were to execute, in the command window,
>> A = 5;
>> B = [6 8];
>> C = [3 6 7; 9 11 12];
>> D = eye(max(size(C)));
the workspace browser would look something like
Some Helpful Workspace Commands
The save and clear commands are helpful commands that work with the MATLAB workspace. save lets you save variables from the workspace to a file whilst clear lets you remove variables from the workspace, thus freeing up memory. clear all will clear every variable from the workspace.
m Files
An m-file (or script file) is a simple text file with a .m file extension. In it you type MATLAB commands which can be executed sequentially by executing just the m-file in the command window. For instance, lets copy our commands from above and paste them into an m-file called mfile_test.m. Now lets add the command C - A to the bottom of the m-file (note do not append a ; after the command). We can execute this m-file from the command window and see the output of all of commands within it executed sequentially
>> mfile_test.m
ans =
-2 1 2
4 6 7
m Files and Functions
m files can also contain functions. For instance we could write a function that calculates the area and circumference of a circle like
function [A, C] = circle(r)
A = pi*r*r;
C = 2*pi*r;
end
and put it in an m-file called circle.m. We could then use this like
>> circle(5)
ans =
78.5398
where ans now holds the area of a circle with a radius of 5 or
>> [A, C] = circle(5)
A =
78.5398
C =
31.4159
where A holds the area of the circle and C the circumference.
Simulink Models
Simulink is a graphical tool that can be used to model various different types of systems as well as modelling dynamic system behaviour. Simulink is closely embedded into the MATLAB ecosystem which adds additional power to it (again I'll come back to this later when describing the similarities).
Simulink models contain blocks which all exhibit different behaviour. There is also the functionality to create your own Simulink blocks should you need. Models in Simulink are made by connecting blocks of different kinds to emulate the system you want to model.
Simulink is quite extensive and I don't recommend learning it on your own. The best way to learn Simulink is invariably as part of a course, Control Systems or Systems Analysis type courses are ideal for this purpose. For that reason I'm not going to dwell on it for much longer here.
How are the MATLAB Workspace, m Files and Simulink all Related?
Well, the MATLAB workspace is available to both m files and Simulink. Variables in the MATLAB workspace can be used in both m files and Simulink.
m files that contain only scripts (and not functions) will write every variable created in them to the MATLAB workspace. m files that contain only functions won't write any of the new variables used in those functions to the MATLAB workspace. The only way a function will alter variables in the MATLAB workspace is by assigning an output value to ans or if you explicitly declare output arguments when calling the function.
Simulink and the MATLAB workspace have a very similar relationship to m files and the MATLAB workspace. Any variable in the MATLAB workspace is available for use in Simulink (in any part, including configuration). For instance in the below configuration I use the MATLAB workspace variables start_time, stop_time and step_time to set up the parameters of model. Typically I would define these in an m-file before I run my Simulink model with sim(). This is how we can relate all 3 together.
Simulink can write variables to the MATLAB workspace by adding output arguments to the sim() command. However, as you've found this can be quite nasty to navigate and extract what you really want! A better approach would be to use the Data Import/Export options in Simulink coupled with a To Workspace block to grab the outputs that you are concerned with so that you can ignore everything else.
Below I have a screenshot of the Simulink Data Import/Export pane. You can see there are options in here for us to send variables to the MATLAB workspace. Typically, the most common you would need is tout which will be a range given by start_time:step_time:stop_time from the earlier configuration pane. Other areas that would be of primary interest in this screen would be xInitial and xout which are used in Root Locus analysis.
However, all that aside one of the best blocks in Simulink is the To Workspace block. This block can be used to store variables directly in the MATLAB workspace and is one of the keys to being able to link MATLAB and Simulink. You get the power of Simulink but the computational and plotting abilities of MATLAB. I've included a screenshot of it below as well as a typical configuration I would use. The default Save Format for this block is Timeseries, however, I recommend changing this to Array as it will make your life much easier.
OK, where was I? I feel like I'm giving a lecture instead of writing an answer!
A Practical Explanation
Simulink Model
So now let's put everything we've learned into practice with a simple example. First we are going to create a simple Simulink model like this
Now we'll set up our configuration for this, as before for the Solver section
We'll also untick Limit data points to last: in the Data Import/Export section
And, that's it. Our simple Simulink model has been created and setup. For the purposes of this answer, I'm saving mine as stackoverflow_model.slx.
MATLAB Script
Now we'll create simple MATLAB script (m-file) called stackoverflow_script.m that will set up the necessary variables for our Simulink model by adding them to the MATLAB workspace. We'll then call our Simulink model and check what new variables it added to the workspace. And, finally we'll generate a simple plot to show the benefits of this approach.
So here is the MATLAB script
% Script developed to describe the relationship between the MATLAB
% workspace, m-files and Simulink
close all
clear all
% Initialise variables
start_time = 0;
stop_time = 10;
step_time = (stop_time - start_time) / 1000; % Creates 1000 + 1 points
% Choose k
k = 60;
% Execute Simulink model
sim('stackoverflow_model');
whos % To display variables returned from Simulink
% Plot results
figure;
plot(tout, yout, 'r');
title('Sample Plot');
xlabel('Time (s)');
ylabel('Output');
Putting it All Together
Now we execute this script in the command window with stackoverflow_script and sit back and marvel at the POWER of MATLAB and Simulink combined.
>> stackoverflow_script
Name Size Bytes Class Attributes
k 1x1 8 double
start_time 1x1 8 double
step_time 1x1 8 double
stop_time 1x1 8 double
tout 1001x1 8008 double
yout 1001x1 8008 double
We can see from the output above that all of the variables Simulink needs (k, start_time, step_time and stop_time) are all in the MATLAB workspace. We can also see that Simulink adds 2 new variables to the MATLAB workspace tout and yout which are simple 1001x1 vectors of doubles. No nasty structs to navigate.
And finally, this produces a nice plot
And so that concludes our whistle stop tour through MATLAB, m files and Simulink! I hope you've enjoyed it as much as I did writing it!
P.S. I haven't checked this for spelling or grammar mistakes so edits are very much welcome ;)
General
Well to put it short workspace is the variable-environement you are working in. If you run a script, your workspace is 'base', which is the same the console uses. So Matlab does have different environements, one is a kind of included environement known as path, the other one is for variables, known as workspace.
Simulink uses a different one, which prevents shadowing variable names I guess.
To check check members in the current workspace use who
To write members to another workspace use assignin
To run something in a specified workspace use evalin
Your Questions
1.
Who lists all the variables in the current workspace you don't need it for the thing you wanna do.
The whole simulink documentation isn't that good...
2.
It does...
3.
If you run a script and define variables they are defined in the base workspace. When you specify a variable in simulink by just entering its name (for example a), it does load it from the base workspace (hence this way arround no problems).
The other way arround is to either use the given export blocks, or specify export values in your own blocks by either using global or assignin.
4.
If you open the scope block and hit the options-buttion (the little gear), you can select an export option. You can aswell specify the type you wan't to use. You seem to use the struct with time option, which is the one with the most lvls, I'd suggest to use the array-type if your problem is just the fact that it is a struct.
You can also just use the Outputblock to specify the export type and name.
So I'd go with:
sim('modelname');
signals=ScopeData.signals;
time=ScopeData.time;
Or when specified as an array:
sim('modelname');
signals=ScopeData(:,2);
time=ScopeData(:,1);
In the example above I don't specify the way the model is run, however you can also specify it as you posted.

Use Matlab PDE toolbox from command line

I would like to solve a PDE with Matlab PDE toolbox using only the command window of the system. I can create the problem and run the solver, but the PDE toolbox window pops up occaisionally and asks questions (e.g., "Do you want to save unsaved document?").
How can I avoid these popups or how can I use the PDE toolbox without opening its window?
I am using the following code. The window is pops up when I call the pdeinit function on the first line.
[pde_fig,ax]=pdeinit;
set(ax,'XLim',[-0.1 0.2]);
set(ax,'YLim',[-0.1 0.2]);
set(ax,'XTickMode','auto');
set(ax,'YTickMode','auto');
% Geometry description:
pderect([0 0.05 0.05 0],'R1');
pderect([0 0.1 0 0.1],'R2');
set(findobj(get(pde_fig,'Children'),'Tag','PDEEval'),'String','R2-R1');
...
The help for pdeinit is short: "Start PDETOOL from scripts." pdetool, like most *tool M-files from The MathWorks, is a GUI and the help/documentaion for it indicates as much.
I'm confused because, not only does pdeinit open a figure window, but you're using it to return handles to the figure and axis of that figure. Your code then proceeds to manipulate those handles. You can't get those handles without first creating and opening a figure. Is the issue that you just want a regular figure window instead? If so, then you can replace [pde_fig,ax]=pdeinit; with:
pde_fig = figure;
ax = gca;
You can look at the code for pdeinit: type edit pdeinit in your command window. You'll see that all it does is open pdetool (unless it's already open) and return handles to the resultant figure and axis.
Additionally, pderect will open pdetool on its own. You're using a bunch of functions that are all tied to the PDE app. Many of the tutorials and examples on The MathWorks's site use this. You might check out this article on how to solve PDEs programmatically. The examples might also be helpful.

Matlab: Saving plot images, override plot.m

Working on MATLAB 2008, I am trying to save all the images my scripts produce when invoking the "plot" function.
In order to achieve this, I have two possible solutions:
Either I write another function having the same parameters and perform a search/replace in the *.m sources
or I override the plot.m file so that I write the image into a specific directory when generated.
I did many searches and I am unable to find the plot.m source file. The only file I found is located in the toolbox directory and does not contain any code (except some commented documentation).
you can simply use the print command and save them into a directory that you can also make using the mkdir command.
Sample code
clc; close all; clear all;
x = 1:10;
y = x.^2;
plot(x,y)
if exist('plots','dir') ~= 7
mkdir('plots'); % make directory if it does not exist
end
print -dpdf ./plots/jawn.pdf
Read the print documentation, to learn how to print in other file formats
Also, I would not suggest overriding the plot command, and you will likely not be able to find the source code for plot.m because that is proprietary MATLAB code

How do I tell my MATLAB GUI that I want to plot on it using an external .m file?

I have a GUI(made using GUIDE) in which there is an axes on which I can draw. When I saved the gui, i have a .fig file and a .m file (whose names are start_gui.m and start_gui.fig). Now, I am trying to plot on these axes using an external M file, to which I have passed the GUI handles. This is as follows:
function cube_rotate(angle1,angle2,handles)
gcf=start_gui.fig; %this is the name of the gui.fig file in GUIDE
set(gcf,'CurrentAxes',handles.cube_axes)%this allows us to plot on the GUI
%plot something
end
handles.cube_axes is the name of the handle in the GUI created using guide. Inspite of passing the handles, it won't allow me to plot in the gui. It throws up an error saying:
??? Undefined variable "start_gui" or class "start_gui.fig".
start_gui.fig is the name of the GUI figure that was generated in GUIDE. How do i make it plot in the axes in start_gui.fig?
Thanks for all the help!
You've made a few errors. The first is referring to a file name without single quotes to denote a string. The second is trying to open an existing figure by assigning it as a variable named gcf. This will just give you a variable gcf which contains the string 'start_gui.fig'.
Instead, open the figure with this command:
fH = hgload('start_gui.fig');
% Then find/assign the axes handle (assuming you only have one axes in the figure):
aH = findobj(fH,'Type','axes');
% And finally plot to the axes:
plot(aH,0:.1:2*pi,sin(0:.1:2*pi));
On a secondary note, is there a reason you're not using the M-file generated by MATLAB to carry out this functionality? By using the auto-generated M-file, you'll be able to access the handles structure rather than using findobj.
The error you're getting is because of your second line: gcf=start_gui.fig;
It's looking for a variable named start_gui, which you don't have. (start_gui.fig is a filename, not a variable.)
To solve your plotting problem, take a look at this Mathworks support article.