How to find all MATLAB function blocks in a Simulink model - matlab

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.

Related

Relate outputs of parent function to input of nested function

I am going to try and explain myself simply in the hopes of getting a simple answer.
Let's say I have a function 'calculate' that takes the inputs [t,k,r,x] and outputs [A,B,C,D] as follows:
function [A,B,C,D] = calculate(t,k,r,x)
Now lets say I have another function that takes these outputs as the inputs, and spits out more, different outputs, eg.
function [M,N] = again(A,B,C,D)
How do I link [M,N] to say k and t? The overall aim is to minimise both M and N by optimising k and t, and I can guess that it has something to do with nested functions and passing parameters but I'm not sure how to start, and the start is all I want. Thanks
Take a look at the Matlab optimization toolbox. It provides functions for a multitude of optimization problems. Although I believe these functions only take one function as a parameter. Therefore for your case it would probably be best, if you do it like this if you can:
Write function calculate.m with parameters (t,k,r,x) and save it.
Write function again.m with parameters(t,k,r,x) and save it.
Function again calls function calculate with parameters (t,k,r,x) and then continues to determine (M,N) from the output of calculate.
in the Matlab toolbox optimization function, e.g.: fmincon(fun,x0,A,b), you then have to use again.m as the function you want to optimize (fun).
Hope that's good enough for a start.

MATLAB execute function for multiple variables in loop

To process data in MATLAB I have to execute a certain function, let's call it function(). Since there is much data to be processed, like large array Time or Voltage (but many more) I execute those one by one like this:
TimeNew = function(Time);
VoltageNew = function(Voltage);
... etc
So this is done around 10 times. Moreover, I have to do such a thing multiple times, resulting in around 30 lines of code which all do the same thing but to a different variable.
Is there a way to optimize this? I am using the most recent version of MATLAB (2015b) and have all toolboxes installed.
A possible solution could be to store the input array into a struct, them use that struct as input of the function.
In the function you can identify the number and content of each field by using fieldnames and getfiled built-in function.
The function could return a structure as output whose names can be made the same as the ones of the input struct.
In the example below, three arrays are generated and the function siply compute their square.
var_1=1:10;
var_2=11:20;
var_3=21:30;
str_in=struct('var_1',var_1,'var_2',var_2,'var_3',var_3)
str_out=my_function(str_in)
The function
function [str_out]=my_function(str_in)
f_names=fieldnames(str_in)
n_fields=length(f_names);
for i=1:n_fields
x=getfield(str_in,f_names{i})
str_out.(f_names{i})=x.^2;
end
Hope this helps.
Qapla'
You could try cellfun
allResultsAsACell = cellfun(#function, {Time,Voltage,...,varN});
This is equivalent to
allResultsAsACell{1} = function(Time);
allResultsAsACell{2} = function(Voltage);
...
allResultsAsACell{N} = function{VarN};
The issue is just matching up the indices with the values. I'm sure you could code those in as well if you needed (e.g. timeInd = 1; voltageInd =2; ...)
To see more on the cellfun method, type
help cellfun
into your MATLAB terminal.

How can I create multiple inputs for a matlab function block?

I want to restrict the variable that I use as input for a matlab function block, it must be only able to increase.
To achieve that i have tried to compare the variable and the previous sample of it in a matlab function, but i don't know how to create two inputs. To solve that i've tried to use a mux, but then i get an error. And google doesn't give me an explanation how to use a mux signal as input for a matlab function.
So that leaves me here with this low-level question.
Thanks in advance for your help and time. Cheers.
To use multiple variables in a function, you need to modify your function declaration at the first line of your function. The reference syntax is:
function [y1,...,yN] = myfun(x1,...,xM)
where x1 through xM are inputs. Your declaration with two inputs might look something like:
function [returnValue] = hasIncreased(previousSample, variable)
See the Matlab Function Documentation for more information.

Why is this error happening: Index exceeds matrix dimensions

I am running a GUI system in MATLAB and I am a beginner with working with GUI's.
The code is extensively long and so I am going to just put in what and where I have things and see if it is enough information for help to be given, thanks.
in my first GUi I have this in the opening function:
HW12_result_bhanford(handles.scan_age, handles.check_athlete, handles.radio_male, handles.radio_female)
this is supposed to be transferring these four variables over to my third GUI named
HW12_result_bhanford
In my second GUI I have this written in the opening function:
age = varargin{1}
athlete = varargin{2}
male = varargin{3}
female = varargin{4}
I then use these four variables(age, athlete, male, female) later in the second GUI and I
assume them to be the equivalent value of the corresponding variable passed from the first
GUI.
When I run everything the error that comes back is Index exceeds matrix dimensions.
if anyone could help me, that would be awesome. If you cannot help without the entire code I understand.
You use varargin if your argument list is variable, and varargin has to be in your function definition.
function HW12_result_bhanford(varargin)
In this case function receive a cell array as an input, so you can get individual arguments with varargin{1} etc.
If you put your arguments together as a structure, you can pass this structure as an argument along.
function HW12_result_bhanford(handles)
But if the function definition has individual arguments, for example,
function HW12_result_bhanford(age, athlete, male, female)
you cannot use varargin, just process the arguments as is.
Read more on how to use VARARGIN.

When using a multiple-output matlab function, do i need to callback all variables?

When using a multiple-output matlab function, do i need to callback all variables? or can I just take the first two variables? (if so..is it not recommended?)
lets say in function.m
[a, b, c] = function( )
in main.m
[var1, var2] = function;
When calling (almost) any function in matlab you can request fewer outputs than it specifies. So, yes the example you give should work perfectly fine.
There are some clever things you can do with this, such as using nargout within a function to see how many output arguments have been requested and only calculating the values that have been requested as an optimisation trick.
It depends on the definition of the function, and exactly which of the outputs you want to get.
Not all the function allow to do it, you can find all the options for each function in the beginning of the help documentation on the specific function.
If you want only the 2nd, or 3rd outputs, and you want also to save the computation-time of the results that does not interesting, you can use ~ option, like this (for versions 2009b and later):
[~, var1, var2]=function
Many functions allow for options to passed that change how the function behaves. I used/wrote various numerical solving functions a bit and one that nice amount of option, for instance is the LSMR function(s).
Otherwise, if you can manipulate the original either introduce an input(s) to do so before or at the end with an inline subroutine to generate the outputs you want.
Or if you can't it will return as either a cell array or a vector and you can pass an anonymous function to generate the desired outputs that way.
Really, can be done many ways. Very contextual.