Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to set parameter values in some components with Matlab API #2140

Open
samuelkarnell opened this issue Sep 1, 2022 · 1 comment
Open
Labels

Comments

@samuelkarnell
Copy link
Contributor

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

  1. Add a Constant and connnect it to a Scope in Hopsan
  2. Save the model
  3. Make sure to have the system variable HOPSANHOME
  4. Run the following code in Matlab:
    addpath('C:\Program Files\Hopsan\Scripts\Matlab')
    obj = hopsan();
    loadModel(obj,'...hmf') %link to the saved Hopsan model
    setParameter(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.

@robbr48
Copy link
Contributor

robbr48 commented Sep 7, 2022

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.

@robbr48 robbr48 added the wontfix label Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants