Iterate over array from workspace at each sample time in Simulink MATLAB function block - matlab

I have a MATLAB function block embedded in a Simulink model. In my initFnc callback I set up some vectors which I need to use in my function block inside the simulink model. The vector is 1x10000 and contains setpoints for a robotic arm. The function block simply needs to read the next value at each sample iteration.
So far I've tried using "From Workspace" and "evalin()" but they all throw various errors when building the model (I'm using code generation which doesn't play nice all the time).
What would be a good way to read in that vector in Simulink and feed the cells one by one into my function block? Something like repeating sequence stair but without the repeating part.

I got it to work by reading in the vector into a constant block and feeding that into my embedded function. I then use a persistent iterator which is a 1x1 matrix (ones(1)) and incrementing it every time the value from the vector is read.

Related

Simulink MATLAB function block with vector inputs

I'm new to Simulink and am working on a project.
The bigger picture is that I want to gather different sensor data, pick out an interval of that, and in my Matlab functions analyze it, a kind of adaption. It feels like I have a fundamental misconception since I thought my Matlab scripts could work directly in Simulink. I have a couple of Matlab function blocks which in Matlab takes vectors as inputs, but when I use a source such as "from workspace" I get the error that it cannot handle more than one value at the same time: Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.
So I wonder if there is any neat way of collecting a given interval of a continuous flow of sensor data (as a beginning, the raw input will just be a vector which will represent sensor data) so that it could be analyzed by some Matlab functions?

How to load non timeseries data into simulink for use in a deep neural network

I trained a neural network in python and want to load the weights into simulink to pass into a function that will build the network. I thought I could use from workspace but it seems it does not handle non time series data for a structure. I get the error.
Invalid structure-format variable specified as workspace input in 'PMSM_FCS_MPC/From Workspace'. If
the input signal is a bus signal, the variable must be a structure of MATLAB timeseries objects.
Otherwise, the variable must include 'time' and 'signals' fields, and the 'signals' field must be a
structure with a 'values' field.
How can I pass a bunch of arrays into a simulink function?
Everything I have seen is about time series data. Nothing on a group of matrices such as would be used in a deep neural network.
The From Workspace block is designed to work with time-series data (as per the error message you show.) There is no need to use it if you have constant data.
If you have non-time series data and you need it as a Simulink signal then use the name of the MATLAB variable as the parameter in a Constant block. The signal coming out of the block will have the value of your data.
If you are using a MATLAB Function block then you can also input the data as a parameter argument to the function. See Add Parameter Arguments for the steps to do this.
Not sure how you want to use the data. But if you have those weights as an array in the main workspace, you can simply reference to that variable in Simulink. For example if you have K = [1 2] in your workspace, you can use any block and type in K(1) or K(2). You can do also matrix operations and gather signals with Mux block.

Sample sequence of points from continuous signal simulink

I have a Matlab function (created by me) that must be evaluated only at a given rate. I would like to sample the value of a signal, give to this function (discrete values) and then, the calculated output must be hold until the next value is available. Is there a way in simulink to do this? All answers I have found use quantizer + ZOH but in this case I still get "a continuum" (or almost it) of points to be evaluated by thsi function which is really slow. Changing the rate of simulink's solver is also not an option as the result of this function will be given for a continuous time system.
Any help will be highly appreciatted!
Thank you
Assuming by Matlab function you mean a MATLAB Function block, then it sounds as if all you need to do is make the block discrete. Do that by right-clicking on the block, going down to Block Properties and then in the resulting dialog enter your required sample time.
The block will then sample its input and generate an output (which is held between sample times) at each sample time.

Heterogeneous data from workspace into Simulink

I have different matrices to import into a Simulink Matlab function from the workspace. These matrices have all different dimension, which I don´t know at priori.
At the beginning I tried using the block 'constant' putting the data all together in a structure like this:
But then, I cannot pick the right matrix since I don´t know the dimension of each element (and also 'mux' cannot be used to split matrices).
I think I will have the same problem also with the block 'from workspace'.
I was wondering if there is a smart way to import heterogeneous structures like these. I tried also with cell-arrays, but it seems to be not supported by Simulink.
Thanks for any suggestions.
If the data is to be used in a Matlab Function block you could define the workspace matricies as parameters in the model explorer and in the Matlab Function port editor. You then have them accessible inside that function without even needing the "const" blocks or drawing any signals.
Even if your final intent is not to have data into a Matlab Function block those blocks are quite useful for extracting signals from heterogeneous data since you can do some size/type checking in them. Then you can output "simulink friendly" signals for use elsewhere.

Matlab Function inside simulink

I need to use a Matlab function inside of a Simulink model. I know how to use a Matlab function to do simple stuff. But what I need now is a little bit more complicated. Let me give you a basic example.
Assume that I need to have a block to generate a sine wave to be viewed directly on the scope (I know that there is already a sine-wave block, I'm just taking that as an example). If I'm writing in Matlab NOT in Simulink, I would do something like:
t = [0:1/30000:0.2];
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
plot(t(1:100),y(1:100))
How can I build the same function inside a Simulink matlab-function block and see the results directly on the Scope?
Remember: The Matlab Function Block has two ports, u and y. Which represents input and output respectively. In the above-given example, a sine-wave generator doesn't need an input.
Perhaps it's just that you haven't chosen a very good example, but there are several things to be aware of when translating the code you've given into Simulink.
The easiest way to get the simulation time into a MATLAB Function block is by feeding a Clock block into an input port (which as #Daniel indicates, are optional, but in this case I suggest would be used for t). So I think you do want an inport in this example.
Your use of plot in your example only plots the first 100 points, where as a Simulink Scope rolls through the data being displayed. There's no concept of only displaying the first X points in a Scope when the simulation runs for longer than that.
You need to remember that Simulink generates data one simulation time step at a time, so you can't generate them all (as per your MATLAB code) and then plot them all. (Well... you can if you want to use frame based signals, but I assume that's not what you're asking here.)
So, to implement the equivalent of what you have would involve doing the following.
write a MATLAB Function block containing the following code (although you might want to make A and f input parameters rather than hard coding them)
function y = myCustomSineWave(t)
A = 1;
f =10000;
y = A*sin(2*pi*f*t);
Feed a Clock block into the above block, and have a Scope block on its output