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

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

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

Thank you for the quick reply! The problem is that the output from these are going into matlab functions and used in masked subsystems for ease of use. When one click on the subsystem, mask prompts the usual way, asking for parameters such as "Speed: ". Now for ease of use I would like that, when one builds the model with these subsystems it could be tested easily with just scalar values such as "5" and later when used with real data, one could just replace on the mask a variable from workspace such as "speed". This current subsystem is functional with scalars without accessing the matlab workspace at all and I would like to keep it that way. Once you want to simulate with real data, you just need to load the variables to workspace and replace the "5" with the corresponding variable. To my understanding if I replace the 1. model with From Workspace, one needs to always create variables to workspace when building a model even with just scalars.
Well yes, you need to create variables to workspace, but you can do it automatically by using File->Model Properties->Model Properties->PreLoadFcn where you can write down your variables and at every opening of your model, the variables are automatically loaded. Just keep in mind.
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!
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

Asked:

on 16 Jan 2018

Commented:

on 16 Jan 2018

Community Treasure Hunt

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

Start Hunting!