MATLAB/Simulink - programmatically supply multiple external inputs - matlab

I have the following Simulink model:
I would like to externally provide inputs u[k] and y[k], i.e., I will be running simulations via MATLAB command line. I found previously that I could set the [LoadExternalInput and ExternalInput][3] options, and they default to the vector [t u].
But my u[k] and y[k] are vectors, and it looks like the ExternalInput can only specify one vector. So each row of [t u] is the value of the entire vector u at time t.
The sizes of u[k] and y[k] in my model here are not necessarily known ahead of time. Is there a way to pass in these vectors (as structs, perhaps)?

From Importing Data to Root-Level Input Ports I've found that I could do something like
sim('myModel', 'LoadExternalInput', 'on', 'ExternalInput', 'u, y');
where u and y are structures with fields time, signals.values, and signals.dimensions; each row of signals.values is a vector corresponding to a n element of time. signals.dimensions is the dimension of signals.values. I have to manually set the In1 ports to expect the same dimension as u.signals.values (well, I can of course do it programatically before hand..):
Note You must set the Port dimensions parameter of the Inport or the Trigger block to be the same value as the dimensions field of the corresponding input structure. If the values differ, an error message is displayed when you try to simulate the model.
(from "Importing Data Structures to a Root-Level Input Port")
What's the point of setting signals.dimensions if I have to set the dimension on the In1 block manually anyhow? Anyway that might have to be how I do this: just examine u and y before running the simulation, then setting the Inblock properties (programatically, of course) to expect vectors of that length.
I am still hoping there is a more elegant solution for this.

Related

Matlab/Simulink LookupTable with Workspace vectors

I am new with Simulink and I am struggling with the Dynamic Lookup Table (inputs : x, xadta, ydata; output: y).
I have several 2D vectors (xdata and ydata) stored in my Workspace and I would like to use them in Simulink in a Dynamic Lookup Table to return a value (y) depending on another variable in Simulink (x).
If I understand how it works I have first to convert my 2D vectors in structures (time,values,dimensions) to be read in Simulink ?
So I did it this way but I got an error :
vector.time = xdata; % dimension 1x100
vector.signals.values = ydata; % dimension 1x100
vector.signals.dimensions = [1 100];
save('vector.mat','vector')
Error
"The last dimension of each
'signals.values' field must be the same as the number of rows in the 'time' field."
Besides I am not sure that what I am trying to do is appropriate... I use the xdata of my vectors/structures as "time" in the structures to get my vectors readable in Simulink. But I do not think it should have anything to do with time notion. I just want the Dynamic Lookup Table to return the "ydata" value of the vector/structure corresponding to the value of "x"="xdata". Only "x" change with time in the Simulation.
It looks like you should just be using the 1D Lookup Table, with your xdata and ydata variables (defined in your MATLAB Workspace) used as the block parameters.
As to the error you're getting, it seems to be related to using the From File block (which it looks like you're using to get data into your model) not the look-up table itself. To get that to work, define your time vector as a column vector, not a row vector as you've done, and think of each row as a different time point.
At each time point, you'll get a different signal value. This is like a look-up table itself (looking up the signal value for each different time value), but doesn't sound like what you are really wanting to achieve.

How to access the variable range for leaf node in classregtree of matlab?

I used classregtree to fit a tree to my data set in order to classify the data. All of predictors and the response are quantitative. I want to save the range of each variable on terminal nodes, because I am gonna use those ranges in another function.
So is there any way that I can have access to those ranges? I can see the variable ranges in view(tree) plot but I need to save them in like a matrix to use them.
I am not totally sure that this is what you were asking for but this gives you the split criterions for all trees
B = TreeBagger(nTrees,M,tag, 'Method', 'classification','OOBPred','on');
view(B.Trees{1:B.NTrees})
where M is your trainig data set and tag are the classes.

Loading data to simulink (Invalid matrix-format)

When I'm trying load data, from Matlab to Simulink, I get this error:
Error using TSFPnew (line 191)
Invalid matrix-format variable specified as workspace input in 'modelTSFP/From Workspace5'. The matrix
must have two dimensions and at least two columns. Complex signals of any data type and non-double
real signals must be in structure format. The first column must contain time values and the remaining
columns the data values. Matrix values cannot be Inf or NaN.
I have very simple model (I know, it be easier to do this computation on Matlab, but this is only fragment of my model):
All data have these same dimension 1x144:
Why I can't just load it to the Simulink space?
The error message is pretty self-explanatory: the data in the From Workspace block represents a time dependent variable so if you are using an array, the first column of the array must be the time values and the second (or more) columns the corresponding data points. Check the documentation for more details. Your data appears to be only vectors, where is the corresponding time data for your values?
If you want a parameter (that doesn't vary with time), then don't use a From Workspace block, use a Constant block instead.

S-function documentation that "S-function level-1 supports vector inputs and outputs. DOES NOT support multiple input and output ports"

I read in S-function documentation that "S-function level-1 supports vector inputs and outputs. DOES NOT support multiple input and output ports".
Does the second sentence mean the input and output dimension must be the same?
I have been using S-function level-1 to do the following:
[a1, b1] = choose_cells(c, d);
where a1 and b1 are outputs, c and d are inputs. All the variables are having a single value, except d is an array with 6 values.
Referring to the image attached, we all know that in S-function block, the input dimension must be SAME as output dimension, else we will get error, in this case, the input dimension is 7 while the output dimension is 2, so I have to include the "Terminator" blocks in the diagram for it to work perfectly, otherwise, I will get an error.
My problem is, when the system gets bigger, the array d could contain hundreds of variables, using this method, it means I would have to add hundreds of "Terminator" blocks in order to get this work, this definitely does not sound practical.
Could you please suggest me a wise way to implement this?
http://imgur.com/ib6BTTp
http://imageshack.us/content_round.php?page=done&id=4tHclZ2klaGtl66S36zY2KfO5co
Updated: actually I have been trying to convert my level-1 S-function to level-2 but I got stuck at calling another sub function at function Output(block) trying to look for other threads but to no avail, do you mind to provide related links?
My output depends on a lot processing with the inputs, this is the reason I need to call the sub-function in order to calculate and then return output values, all the examples that I can see are calculating their outputs directly in "function Output(block)", in my case I thought it is not possible.
I then tried to use Interpreted Matlab Function block but failed due to the output dimension is NOT the same as input dimension, also it does not support the return of more than ONE output................
Level-1 s-function supports single input and single output port. These ports must be vectors. But there is no restriction on the length. Input and output can have different lengths. You can use selector block to select only relevant data. You do not need to use Bus in the output.
There is also no restriction on calling other sub-functions from Output. If your sub-function is not in the same file it must be in the path or in the current directory.
If your MATLAB code is compatible with MATLAB Function block, I recommend using that block. It is simpler to setup and use.

Can you explain this Embedded MATLAB Function error?

I'm having a problem sending a value from a GUI to an Embedded MATLAB Function (EMF) in a Simulink model. I get this value from a slider in my GUI and send it to an EMF block in my model. I can confirm that the value is being transferred correctly from my GUI to my Simulink block, since I can display the value with a display block in my model and see the value change when I change the slider position in my GUI. However I keep getting this error when I run my model:
Could not determine the size of this expression.
Function 'Kastl' (#18.282.283), line 14, column 1:
"f"
This is part of my EMF block code:
function y = input_par(u,fstart)
...
f_end = 1000;
f = fstart:f_end;
...
I believe MikeT is correct: you can't redefine the size of a variable in an embedded function. If you look at this Embedded MATLAB Function documentation page under the subsection Defining Local Variables, it says:
Once you define a variable, you cannot
redefine it to any other type or size
in the function body.
You will have to rework your embedded function such that the variables you declare are not changing size. Since I don't know what you are subsequently doing with the variable f, there's not much more specific help I can give you.
In general, if you absolutely need to use data that changes size, one solution is to pad the data with "garbage" values in order to maintain a constant size. For example:
MAX_ELEMS = 1000; % Define the maximum number of elements in the vector
f = [fstart:MAX_ELEMS nan(1,fstart-1)]; % Create vector and pad with NaNs
In the above example, the variable f will always have 1000 elements (assuming the value of fstart is an integer value less than or equal to 1000). The value NaN is used to pad the vector to the appropriate constant size. Any subsequent code would have to be able to recognize that a value of NaN should be ignored. Depending on what calculations are subsequently done in the embedded function, different pad values might be needed in place of NaN (such as 0, negative values, etc.).
I believe the issue you are running into is that you can't change a parameter during simulation that will cause the dimension of a signal to change. In your example, the code,
f = fstart:f_end;
changes size whenever fstart changes. I think this is what EMF block is complaining about. I don't have any easy workaround for this particular issue, but maybe there's an equivalent way of doing what you want that avoids this issue.