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.
Related
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?
I am aware that .p files are Matlab protected files. So, I am not trying to access them. However, I was wondering if I could use their output onto the Matlab shell as input to a Matlab program.
What I mean is the following: I have to simulate a dynamic system in Matlab using a controller. Afterwards, I need to assess its performance. This is done by the .p file. Now, the controller behaviour is defined by six distinct variables. I pretty much know their range. So, what I did was create an optimization to find the optimal coefficients. However, when I run the .p file I see that the coefficients I obtained as optimal are in fact not optimal, i.e. my cost function is biased in some way.
So, what I would like to do is to use the output of the .p file (there are always six strings with only two numerical values - so they would be easy to extract if it were a text file) to run a new optimization so that I can understand what I did wrong in my original cost function.
The alternative is finding the parameters starting from my values by trial and error, but considering there are six variables I would prefer a more mathematically pure approach.
Basically, the question is how I can read the output onto the command prompt of a Matlab .p function, and use it as input in a Matlab function.
Thanks for the help!
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.
I have a MATLAB Function Block where I compute X variables that I want to use later in a script.
My first option was to use To Workspace blocks for each of them, but since I have X variables, it fills up the model with blocks. The problem is that these X variables have different dimensions, otherwise I could just build a matrix.
My idea was to build a structure within the MATLAB Function Block and then use a single To Workspace block, but my research told me I would need a Simulink.Bus object to do so.
Is this approach at all the best way to achieve my goal? Thanks in advance for your input!
I have a, in my opinion, little problem using a Matlab-Function Block in a Simulink Model.
I want one of the inputs to be a structure.
But that doesn't work so easily, I've come across
suggestions on other semi-related problems, which tell about defining Simulink.Bus and so on,
but to be honest I don't understand how this can be applied on my problem.
It seems simple:
Structure as input for a Matlab function Block
But I don't know how.
Sorry for the inconvenience.
Regards BZAD
Within Simulink you create a Bus signal using the Bus Creator Block. This can then be fed into a MATLAB Function block, but only if an accompanying Simulink.Bus object is created in the Base Workspace.
See Types of Structures in MATLAB Function Blocks and Create Structures in MATLAB Function Blocks and the links off those pages for more information.