Clear Filters
Clear Filters

mathworks.maab.na_0022: What kind of Parameters can I use as constant values for Case arguments in Matlab function blocks?

2 views (last 30 days)
Hello All,
According to "na_0022: Recommended patterns for Switch/Case statements" rule, parameters and enumerations can be used as constant values for case statements:
function outVar = NA_0022_Pass(SwitchVar)
%#codegen
switch SwitchVar
case Case_1_Parameter % Parameter
outVar = 0;
case NA_0022.Case_2 % Enumerated Data Type
outVar = 2;
otherwise
outVar = 10;
end
end
What kind of Parameter is mentioned in this example? Is it a Simulink.Parameter? If yes, how should I declare it?
My first thought was, that 'Case_1_Parameter' is a masked parameter, but if Matlab function blocks are masked, parameter arguments shall appear in the function header, and in the example above there is no such an argument. I also tried to add Simulink.Parameter objects to the model and base workspaces, the model could not compile saying "Undefined function or variable 'Case_1_Parameter".
Thank you.
--
Best Regards, Evgeny

Answers (1)

Sanchari
Sanchari on 16 Feb 2024
Hi Evgeny,
I understand that you want to know the type of parameter that has been used in the example code given here, https://in.mathworks.com/help/simulink/mdl_gd/maab/na_0022recommendedpatternsforswitchcasestatements.html?s_tid=srchtitle .
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; % Assign the value you want to use in the switch-case
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'); % Replace ‘your_model_name’ with the actual name of your Simulink model
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:
  1. 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
  2. Enumeration definition: https://in.mathworks.com/help/matlab/matlab_oop/enumerations.html?searchHighlight=enumeration&s_tid=srchtitle_support_results_2_enumeration
  3. 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!

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!