Hi Evgeny,
In the context of the switch statement within a MATLAB function block in Simulink, the guideline suggests that the case arguments should use constant values. These constants can be defined in several ways, including:
- Literal constants (e.g., 1, 2, 'a string')
- Enumerated data types (as shown in the code with NA_0022.Case_2)
- Simulink.Parameter objects
In the given example, 'Case_1_Parameter' is a placeholder for a constant value that should be defined elsewhere in the model. If you want to use a Simulink.Parameter object as a constant value for a case argument, you need to define it in the MATLAB base workspace or in the model workspace. Here's how you can define a Simulink.Parameter:
Case_1_Parameter = Simulink.Parameter;
Case_1_Parameter.Value = 1;
Case_1_Parameter.CoderInfo.StorageClass = 'Custom';
Case_1_Parameter.CoderInfo.CustomStorageClass = 'Const';
After defining the parameter, you can use it in your MATLAB function block as a constant. Ensure that the parameter is defined in a workspace that is accessible by the MATLAB function block. If you define it in the base workspace, it will be globally accessible. If you define it in the model workspace, it will only be accessible within that specific model.
Here's how you can add a parameter to the model workspace:
modelWorkspace = get_param('your_model_name', 'ModelWorkspace');
modelWorkspace.assignin('Case_1_Parameter', Case_1_Parameter);
It's important to note that when using Simulink.Parameter objects in a switch statement, you must ensure that the code generation settings are correctly configured to treat the parameter as a constant during code generation ('Const' storage class).
Lastly, the error message "Undefined function or variable 'Case_1_Parameter'" indicates that the function block is unable to find the definition of 'Case_1_Parameter'. This could be because the parameter is not defined in an accessible workspace or because the name is misspelled. Make sure that the parameter is correctly defined and accessible in the context where the MATLAB function block is being executed.
Please refer to the following MathWorks Documentations for further information on:
- Simulink.Parameter: https://in.mathworks.com/help/simulink/slref/simulink.parameter.html?searchHighlight=define%20simulink.parameter%20object&s_tid=srchtitle_support_results_1_define%20simulink.parameter%20object
- Enumeration definition: https://in.mathworks.com/help/matlab/matlab_oop/enumerations.html?searchHighlight=enumeration&s_tid=srchtitle_support_results_2_enumeration
- Enumeration: https://in.mathworks.com/help/matlab/ref/enumeration.html?searchHighlight=enumeration&s_tid=srchtitle_support_results_1_enumeration
I hope this resolves your query!