MATLAB Class Method Approach - matlab

I have a class that populates properties which are time-series parameters. The parameters time unit can be seconds of the day (SOD) or run time (RT). I also have methods which plot these parameters. I am looking for the best way to give the user the option to be able to plot in either SOD or RT. Here is what my class looks like:
properties:
AC_Voltage
DC_Voltage
TimeSOD
TimeRT
end
methods:
function me = DataViewer()
% sets the properties
function CalculateSOD(me)
function CalculateRT(me)
function PlotACVoltage(me)
function PlotDCVoltage(me)
end
The class is passed as a field to a GUIDE GUI handle and a button exists for each plot method. What I'd like to do is for example call PlotACVoltage which plots that parameter against SOD, then have the option to call it again and plot it in RT, and have the freedom to be able to switch back and forth. What methods/properties should I setup in support of this? Thanks.

Related

How do I use methods from a class that works in matlab and use it in simulink?

I have defined a class that works in Matlab. I have to instantiate with values and I have 5 different objects. I want these objects in Simulink.
What I wanted to do was initialize them in a Matlab m file and have the objects in the workspace but I could not access them from the Simulink function block. What should I do?
I tried making a Matlab system block but that did not work.
basically what I want ( i have made an example of what class I have):
classdef ExampleClass
%EXAMPLECLASS Summary of this class goes here
% Detailed explanation goes here
properties
Property1
end
methods
function obj = ExampleClass(inputArg1)
%EXAMPLECLASS Construct an instance of this class
% Detailed explanation goes here
obj.Property1 = inputArg1 ;
end
function outputArg = method1(obj,inputArg)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
outputArg = obj.Property1*inputArg;
end
end
end
My class looks like something above. Because it is a big class with many methods I don't want it to run many times so I want to instantiate once.
P1=ExampleClass(1)
P2=ExampleClass(2)
P10=ExampleClass(10)
And then use the above objects in Simulink where the function argument is a value from Simulink so
function [y]=compute(x)
a(1)=P1.method1(x+2)
a(2)=P2.method1(x+2)
a(3)P10.method1(x+3)
y=mean(a)
end
How do I do this? I really hoped I could use my objects from workspace like some variables.
My question boils down to how do I use the class (that specific class as it is similar to mine) in simulink so I can use "P.method1(x)" where x is a value from simulink?

How to get the number of outputs from a handle to a method in Matlab?

You can call methods of an object using function handles. Example:
classdef foo
methods
function value=foofun(this)
value=1;
end
end
end
and then
fun=#foofun;
fun(foo);
The handle is just a string I guess, and it doesn't bother Matlab that it can't tell which function is intended before the handle is used. The same handle could even be used to call different methods if different type objects are used as the first parameter.
To capture the outputs of the function you can do this:
outputs=cell(numberOfOutputs,1);
[outputs{:}]=fun(foo);
However, if the function handle was a parameter in the code, then the number of outputs is a problem. For handles to normal functions nargout would solve that, but now nargout can't help because the handle doesn't determine on it's own which function will be called. nargout would need a second input, the type of the object that is going to be used.
It's a bit inelegant to need both the handle and the number of outputs as parameters. Is there another way to call methods indirectly and still capture all the outputs?
Use-case
Imagine a graph with nodes and edges between nodes. Each edge has a few data objects associated with that particular edge. I'm interested in doing various calculations on those data objects.
Examples
Take all the data objects associated with a specific edge and calculate spectra of those objects.
Use the prettiest data object from each edge and get the spectra from those objects.
Choose ten edges randomly, choose randomly one data object from each of those and then compare the objects with a specific, separate, object.
Design
The idea is to separate the selection of the objects and the calculations into different functions. This way the user can combine any calculation with any selection method. Each time a new method is added to the data objects, it can already be called with the existing object selection methods and each time a new selection method is added, it can be used with all the available calculation methods.
A simplified version of a function I'm currently using looks like this
function [ outs ] = callForEach(objects, fun, numberOfOutputs, extraArguments)
for ii=1:length(objects)
out=cell(1,numberOfOutputs);
[out{:}]=fun(objects{ii},extraArguments{ii,:});
outs(ii,:)=out;
end
end
I'd like to be able avoid the parameter numberOfOutputs. It's an inconvenience for the user and it's more error prone to do it like this.
I guess I could just force a same interface for every method: everybody returns a cell array if they need several outputs. I can then assign that cell array, or whatever it is that is returned, into a single cell. However all the built-in functions seem to prefer to use multiple outputs instead, so there's at least a stylistic difference. If I ever wanted to use one of the built-ins as the handle, I'd need to write a wrapper to make the built-in conform to the interface.
Check this class as foo.m. By running foo.main() you recover most of the addressed questions.
This is not the maximum elegance in OOP, but you have Matlab as is.
Remember the obj variable is MANDATORY, in and out. No obj, no dot fanciness.
The final calls, HAVE to include the f1. Putting a ~ lost not only the fanciness, but the object and its data.
Besides the obj treatment, nargin|nargout|varargin|varargout are valid inside a function body only, and is runtime dependent, as usually. One cannot in general know how much parameters you will throw, because is equivalent to the length of the varargout cell.
classdef foo
properties
value;
end
methods
function [obj,flag]=fun1(obj,this)
obj.value=this+1;
flag=1;
end
function [obj,flag]=fun2(obj,this)
obj.value=2*this;
flag=2;
end
function [obj,varargout]=fun3(obj,varargin)
for i=1:nargin-1
varargout{i}=varargin{i};
end
obj.value=0;
end
end
methods (Static)
function main()
if 0
%% Run this line
foo.main()
end
f1=foo();
[f1,flag]=f1.fun1(10);
flag
f1.value
f=#fun2;
%[f1,flag]=f1.f(10); % fails
flag
f1.value
[f1,flag]=f1.('fun2')(10);
flag
f1.value
[~,flag]=f1.fun2(10); % fails
flag
f1.value
[f1,a1,a2,a3,a4]=f1.fun3('x1',2,[],#(x)(x>1))
end
end
end

How to find all MATLAB function blocks in a Simulink model

I would like to know how to find all MATLAB function blocks in a Simulink model.
In a model in which I know there are a lot of MATLAB function blocks, I tried the following command:
find_system(myModel,'LookUnderMasks','on','FollowLinks','on','BlockType','MATLAB Function')
However, this command returns an empty cell array. If I try something like this:
find_system(myModel,'LookUnderMasks','on','FollowLinks','on','BlockType','Gain')
, I'm getting many results. Is the "MATLAB Function" an actual BlockType or is there another term to use?
You can use the following code to find all MATLAB function blocks.
S = sfroot();
% Get block handles
B = find(sfroot, '-isa','Stateflow.EMChart');
This will search all open models and returns a list of objects of type Stateflow.EMChart. You can look at the Path property of these objects to reduce the list to the model you want.
You will need to define the search depth use the below:
h=find_system(myModel,'SearchDepth',N, 'regexp', 'on', 'FollowLinks','on','Findall','on','LookUnderMasks','all', 'BlockType','Gain');
Specify N, the higher the N the deeper into the blocks the search will be.
Let me know if this doesn't work.

Function handle using `set` function MATLAB

I'm working at GUI in MATLAB application.
I use uitable object. Then I find interesting undocumented feature how to sort it's data, select whole row and etc.
I do this way:
% create jhandle to my uitable object
juiTable = findjobj(handles.uitable1,'class','UIScrollPane');
jtable = juiTable(1).getComponent(0).getComponent(0);
%... some my action like this:
jtable.setRowSelectionAllowed(true);
%...
%and now lets try use callback for selected cell in uitable:
juiFunHandle = handle(jtable, 'CallbackProperties');
set(juiFunHandle, 'MousePressedCallback', #CellSelectionCallback);
set(juiFunHandle, 'KeyPressedCallback', #CellSelectionCallback);
That works perfectly.
Now question: how to put multiple parameters to CellSelectionCallback?
I want this function makes some action (makes some button active etc).
For this I try to put GUI handles to it. But how?
My CellSelectionCallback function:
function CellSelectionCallback(juiTable, varargin)
% get it from the example
row = get(juiTable,'SelectedRow')+1;
fprintf('row #%d selected\n', row);
P.S. I see varargin into it. So can I use multiple arguments? How to put it using my set function??
By default, MATLAB callbacks pass two input arguments (the objec that generated the callback and some event data). If you want to pass more (or fewer) arguments to your callback, you can use an anonymous function to accept these two inputs and then call your callback with the desired inputs.
In your case, you could write your anonymous function such that you pass the handles object as an additional input to your callback function
set(juiFunHandle, 'MousePressedCallback', ...
#(src, evnt)CellSelectionCallback(src, evnt, handles));
Then your callback would look something like:
function CellSelectionCallback(jtable, evntdata, handles)

How to store results from a callback function?

I have created a MATLAB gui in order to run a certain simulation.
In this gui is one button to start the simulation. This button callback function will then excecute the calculations. This will off course result in a dataset with the results.
Furthermore in the interface is a plot area, and a selectbox to switch between different graphs, in order to show different aspects of the simulation results. Therefore the results must be available for other functions in the gui as well. This is a problem, since the callback function has no output
Two solutions I can think of are storing the dataset in a MAT-file, or using global variables. The first solution seems not really correct to me, and furthermore I learned that global variabeles must be avoided if possible. So what is the best solution here?
you could create a user defined class inheriting from the handle class that defines your callbacks, your callbacks then execute from "inside" the handle class instance
classdef mySimulation < handle
properties
hFigure
mySimResults
end
methods
function this = mySimulation(varargin)
hFigure = figure;
...
<build figure components>
...
end
function myButtonCallback(this, src, evnt)
this.mySimResults = runMySimulation;
...
<update plot etc>
end
function mySelectBoxCallback(this, src, evnt)
...
<update plots>
end
end
end
MATLAB offers certain functions for this. There is the function guidata, which can store one variable. This can for instance be used to pass around your gui handles. Furthermore there are the functions setappdata and getappdata. These functions are the way to transfer data between functions, and couple variables to a figure handle.
More information on the different methods can be read here.
This is supposed to be semantically more correct then using global variables. However, I am still curious why. Any comments?