Simulink: Reading complex data from workspace - simulink

When using from workspace in Matlab Simulink to read a complex set of data returns the following error
Unsupported input format for From Workspace block 'untitled/From Workspace1'. Available formats are double non-complex matrix, a structure with or without time, or a structure with MATLAB timeseries as leaf nodes. All formats require the data to be finite (not Inf or NaN).
Is there any work around to solve this issue?

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.

Using output of Matlab protected file as input in Matlab

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!

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.

Dense output of SimMechanics?

I have set up a model using SimMechanics. It outputs data at the times where the solver steps to. Is there any possibility to have some kind of dense output such that it is possible to interpolate these data to get the solution at arbitrary points without losing the high order of the integrator?
In Matlab this is easily possible using the function deval after the integration of one of the built-in ODE integrators.
In SimMechanics I can select these integrators, too. Is there some kind of analouge way to deval?
Yes, it's possible, although it's a Simulink functionality, not specific to SimMechanics. In the Configuration Parameters of the model, you can set the model to Produce Specified Output Only (see http://www.mathworks.co.uk/help/simulink/gui/data-import-export-pane.html#bq9_fhw-1), under Data Import/Export. This way, only the outputs you specify will be produced regardless of the time steps taken by the solver.