Why the constant-block is much slower than from workspace-block when reading from matlab workspace

6 views (last 30 days)
Hello everyone.
I have two following models (image attached with the models circled 1. and 2.) producing the same output with input from the matlab workspace. Model 1 uses input speed with dimensions of Nx1 (constant block). Model 2 uses input speed_time with dimensions of Nx2 (from workspace block), first column being the timetstamp.
Why is the first model so much slower to run than the second model? I prefer using the first model, since the input to the constant block can either be one single number, or a vector variable from the matlab workspace. Do you have any suggestions to make the first model more efficient? For example, how to transform the signal in efficient way, that I can input "speed" (variable from workspace) or "5" to the constant block and it produces the correct output without error? It works now, but it's very slow.
Thank you in advance!

Accepted Answer

Birdman
Birdman on 16 Jan 2018
Well, if you want to know why the first model is slower than the second, just generate C code from two of them by building Build Model and see their difference. As a result, since first model contains more logic and so on, it will form more lines of C code and it will take more time to implement.
Furthermore, From Workspace block is the best option among its alternatives. If you want to give constant input from From Workspace block throughout the simulation, you can create a ones vector with the same length of first column, which contains time data. Something like as follows:
5*ones(numel(time),1)
  4 Comments
Valkmi
Valkmi on 16 Jan 2018
I need to dig in to these callback functions more closely! The problem is that I do not know beforehand what the values are for the variables, it depends on the machine. Is there a way, with the callbacks, to detect the input from the mask to the From Workspace block and create variables accordingly. Say that someone inputs a "5" on the masks speed parameter, the callback creates a variable speed = 5, changes the input to the "speed" without changing the parameter value "5" on the mask. I think the code need to detect whether the input is string or a integer and do the changes accordingly. I once created a model that it detects if the input is string or integer and changes the block accordingly, with integer input the block was Constant and with string input the block changed to From Workspace and connected the blocks accordingly. This was too much work and not modular. My current approach is much easier since it works either with any integer input "5", "15" or "500" or with variable from workspace such as "speed", its just slow.
Again thank you for the quick reply!
Birdman
Birdman on 16 Jan 2018
Is there a way, with the callbacks, to detect the input from the mask to the From Workspace block and create variables accordingly
You may use get_param command for that purpose but note that those commands outputs variables in string format so you need to do a conversion. First set your variable on a mask before simulation and write the command with get_param to StartFcn. That might be a way to do it.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!