Failed to set parameter values in some components with Matlab API
Describe the bug The function setParameter(obj,name,value) in the Matlab API does not work for some components. The problem appears to apply to signal source components such as Constant and Step.
To Reproduce
- Add a Constant and connnect it to a Scope in Hopsan
- Save the model
- Make sure to have the system variable HOPSANHOME
- Run the following code in Matlab:
addpath('C:\Program Files\Hopsan\Scripts\Matlab')obj = hopsan();loadModel(obj,'...hmf') %link to the saved Hopsan modelsetParameter(obj,'Constant.y',num2str(3))simulate(obj)x = getDataVector(obj,'Constant.y.Value');max(x)
Expected behavior The value that is assigned to the constant (3 in this case) from Matlab should show up, but instead it gives the value that is saved in the component in the Hopsan model. The following error message is shown:
- Error: Failed to set parameter value: y
Hopsan version (please complete the following information): Version information is shown in the message widget when HopsanGUI is started
- OS: Windows 10
- Hopsan version: HopsanCore 64-bit, Version: 2.20.0.20220711.1354
- Matlab version: R2021a
Additional information The function has not worked for any signal source component that have been tested, but for other signal components. Nevertheless, a workaround is to use system variables.
This is actually not a bug, but a bit confusing. Hopsan has two types of parameters: constants, and start values for input and output variables. For constants you only need to specify the name of the parameter (e.g. "component.y"). For intput and output variables, however, you need to specify both the port name and the name of the variable in the port (e.g. "component.y.Value").
So in this case, you need to call setParameter(obj,'Constant.y.Value',num2str(3)).
Side note: For signal ports there is of course only one variable, called "Value", but it still needs to be specified. We want the API to be consistent for all port types, and we also support (but advise against) having a constant with the same name as a port.