MATLAB Answers

Dimension Mismatch between Simulink Bus and Calculation Results

14 ビュー (過去 30 日間)
Nate Fulbright
Nate Fulbright 2020 年 3 月 23 日
コメント済み: Nate Fulbright 2020 年 4 月 6 日
I have a Simulink model I created that executes a dynamic model of a hydraulic motor. The dynamic model was originally developed using MATLAB code that uses structures as inputs and outputs to make passing parameters and results much more compact and logical. I've embedded the functions in Simulink using the "MATLAB Function" block.
Following the example in "emldemo_bus_struct", I've converted my input and output structures into buses that have the same organizational structure as the original data structures I was using. This works for all but one of the outputs.
The model is set-up to be able to use a variable number of pistons, and return the results for each piston by resizing an array accordingly. To be able to take advantage of the variable time step solvers in Simulink, I set the "DimensionsMode" of each bus element to "Fixed" and programatically update the dimensions of the bus element before running the Simulink model. The number of pistons is constant for a given run of the model. I can perform the analysis as desired, but when trying to assign the results to the output structure/bus, I get the error "Dimension 1 of field 's' is fixed on the left-hand side but varies on the right ([11 x 1] ~= [:? x 1])." I understand that this error occurs because the size of the array within the function is technically variable, even though the same parameter is used to set both the size of the bus element and the size of the calculation within the function.
Because the underlying analysis is performed using MATLAB code that is used elsewhere, I can't hard-code the number of pistons into the MATLAB function. Additionally, we run the analysis with a different number of pistons on occasion, so having hard-coded values are non-ideal.
I have already tried putting the line "coder.varsize('s');" before preallocating space for the results, but that did not work.
Ultimately, I'd like to be able to force Simulink to ignore checking that the size on the right-hand side (the calculation results) is the same as the left-hand side (the bus), because I know they will be the same size.
Any help on this issue would be greatly appreciated.


Mark McBroom
Mark McBroom 2020 年 4 月 4 日
The easiest approach is to set the variable that holds the result to be a fixed length vector with a length equal to the largest number of pistons you expect to have. This is easier than trying to set up the MATLAB code for varsize, and in the end you have to set up varsize with the max possible length anyway ( Simulink only supports bounded varsize ).
  1 件のコメント
Nate Fulbright
Nate Fulbright 2020 年 4 月 6 日
Thanks Mark!
This method adequately solved my problem, though I wish there was something more elegant.
For others who may want more details, here's what i did:
  1. Created a bus that had piston result vectors sized to contain up to 30 pistons (more than we'll ever use for anything practical). This is hardcoded into the functions so Simulink can determine the size of the output.
  2. The results from the code block have 30 pistons, but only a certain number (say 9 for example) actually have data. Instead of making the end user have to ignore this data, I made a sub-system to remove it, described in the following steps.
  3. The subsystem takes in the output bus ans uses a bus selector to pull out each channel.
  4. A standard "selector" block is used to only pull the first 9 (or however many active pistons there are) pistons from the vectors or matrices.
  5. Before the model runs, I load in all of my custom busses, one of which is the final output bus. I dynamically change the size of the fields in this bus before the Simulink model runs to contain enough space for only the number of pistons I simulate.
  6. I use a bus creator to combine all of the "stripped down" vectors I just made.
This method allows me to return to the user a bus/structure output that has "dynamically" sized vectors. ("Dynamic" for our application means we can change the number of pistons without having to modify the Simulink block or code in any way to change the number of pistons, only parameters files.)


その他の回答 (0 件)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by