Matlab Simulink function - matlab

I am building a reduced order observer in MATLAB. The matrices are calculated using functions/script files outside matlab and simulink function blocks are using these functions to calculate values.
The problem is that some commands like 'acker', 'place' etc which used to work on command window/function/script files are not working in simulink function block and showing errors.
I tried using simin block to take these matrices from workspace but it is also showing errors which I can't understand.
Thanks for your help.

If I get your question correctly then, from User defined functions, you could add a Matlab function block with the following code:
function fcn(in)
%#codegen
coder.extrinsic('acker', 'place')
# Now you can use acker, place so add more code.

Related

MATLAB Function Interface Error

I have trained the neural network in matlab script file and saved the trained data into a .mat file. So that after loading the .mat file in Simulink user defined functions, I could use this trained data to test the inputs.
But I get run time error
Call to Matlab function aborted: Error calling Matlab function 'sim'. Press OK to open the debugger.
and in debugger the error is
MATLAB Function Interface Error: Error calling MATLAB function 'sim'. Block Neural Network Function (#108) While executing: none
The code is as
function [tau1p,tau2p] = Nntwork(theta1,theta1d,theta2,theta2d,theta1dd,theta2dd)
coder.extrinsic('load');
coder.extrinsic('sim');
net=load('trainednet.mat');
a=zeros(1,2);
a=sim(net,[theta1;theta1d;theta1dd;theta2;theta2d;theta2dd]);
if some one could help me to resolve this error.
The first thing I'd check is that your variable net is actually a NN object. The syntax that you are using for load loads all variables in the mat file into a structure called net, and you most likely need to use net.net to extract the net variable from the net structure. (Of course if that's the case then you shouldn't call the structure (i.e. the output from load) net as it will be very confusing.
However, I'd suggest the best way to do this is to wrap you code into another function, and call that function from the MATLAB Function Block. i.e.
Make the MATLAB function block something like:
function [tau1p,tau2p] = NntworkWrapper(theta1,theta1d,theta2,theta2d,theta1dd,theta2dd)
coder.extrinsic('Nntwork');
a=zeros(1,2);
a=Nntwork([theta1;theta1d;theta1dd;theta2;theta2d;theta2dd]);
Then have this additional function in a separate m-file:
function a = Nntwork(theta_data)
load('trainednet.mat');
a=sim(net,theta_data);
A set-up like that will enable you to run and test the NN code independently of Simulink, but also call it from Simulink when required.

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.

How can I get workspace variables in MATLAB Function?

I am using Matlab function in my simulink code where I am using the load command for getting some matrices and variables from the workspace
persistent ProblemParams;
if isempty(ProblemParams)
ProblemParams = load('ProblemParams.mat');
end
This is working well, however there can be problem when I am running multiple simulations at the same time, hence I would like to know what other options do I have to pass an array to this block from MATLAB workspace?
Whether or not the above works, it's not the right way to get data into the block. You should load the variable into the MATLAB Workspace prior to starting the simulation, then pass the variable into the MATLAB Function Block as a Parameter Argument.

Problems with Embedded Functions within Simulink

I'm trying to simulate a very simple model using an embedded matlab function that takes the input and add's 10 to the value using a constant block that inputs into the matlab function, which then outputs to a display block.
As soon as I press simulate I get an abundance of errors. First I get a huge paragraph in orange text stating a warning out the solver 'variableStepDiscrete' instead of solver 'ode45'
Here is the remaining lines that are echo'd from the command prompt:
Code Directory :
"/Users/dazgti/Documents/MATLAB/slprj/_sfprj/embeddedFunction/_self/sfun/src"
Machine (#32): "embeddedFunction" Target : "sfun"
Chart "MATLAB Function" (#49):
.
"c2_embeddedFunction.h"
"c2_embeddedFunction.c"
"embeddedFunction_sfun.h"
"embeddedFunction_sfun.c"
"embeddedFunction_sfun_debug_macros.h"
Interface and Support files:
"embeddedFunction_sfun_registry.c"
Code generation failed Attempt to execute SCRIPT union as a function:
/Users/dazgti/Documents/MATLAB/union.m
I have a script file within my matlab directory called union.m, but I have no idea why its mentioning it.
function y = fcn(u)
%#codegen
x = u + 10;
y = x;
MATLAB Function block works by generating "C" code for the MATLAB code you entered in the block. In the process of generating code there could have been a call to union function in MATLAB from MATLAB Function block infrastructure. Since you have overridden the union function instead of the built-in function MATLAB might have attempted to call your script which caused the error. It is better to avoid naming your functions same as MATLAB built-in functions.

how can this function "resizeColumnscore" resizes image?

I want to know how can this function(from MATLAB) resize the columns of an input image using weights an indices previously computed.
Which equations uses to do that?
resizeColumnsCore(double(in), weights', indices');
When I looked for a function called resizeColumnsCore in MATLAB 7.11.0 (R2010b) I didn't find anything. However, I did find a MEX-file by that name in MATLAB 7.8.0 (R2009a) in this subdirectory of the Image Processing Toolbox:
C:\Program Files\MATLAB\R2009a\toolbox\images\images\private\
I guess they've phased it out or replaced it with another function in newer MATLAB versions. Now, if you want to know what the MEX-file does, you need to look at the source code it is compiled from. Luckily, it appears that this source code resizeColumnsCore.cpp can be found in the following directory:
C:\Program Files\MATLAB\R2009a\toolbox\images\images\private\src\misc\
And you can look through that code to determine the algorithms used to resize the columns of an image given a set of weights and indices.
Now, if you want to know how these input arguments to resizeColumnsCore are computed, you'll have to look at the code of a function that calls it. I know of at least one function in the IPT that calls this function: IMRESIZE. If you type edit imresize at the command prompt it will open that function in the Editor, allowing you to look through the code so you can see how the arguments to resizeColumnsCore are created.
What I can tell you for R2009a is that there is a subfunction in the file imresize.m called contributions which computes the weights and indices that are ultimately passed as arguments to resizeColumnsCore. That is where you will want to start looking to determine what algorithms are used to compute these arguments.
Looks like this isn't a proprietary MATLAB function. Could we see some code or a link to the code?