{"id":11770,"date":"2022-03-31T12:47:40","date_gmt":"2022-03-31T16:47:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/simulink\/?p=11770"},"modified":"2022-04-14T17:54:35","modified_gmt":"2022-04-14T21:54:35","slug":"specifying-variable-values-using-mathematical-expressions-generated-code","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2022\/03\/31\/specifying-variable-values-using-mathematical-expressions-generated-code\/","title":{"rendered":"Specifying Variable Values Using Mathematical Expressions: Generated Code"},"content":{"rendered":"<div class = rtcContent><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span style=' font-style: italic;'>Some time ago, I published a post where I described <\/span><a href = \"https:\/\/blogs.mathworks.com\/simulink\/2022\/01\/19\/specifying-variable-values-using-mathematical-expressions\"><span style=' font-style: italic;'>how to specify variable values using mathematical expressions<\/span><\/a><span style=' font-style: italic;'> using the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/simulink\/slref\/slexpr.html\"><span style=' font-style: italic; font-family: monospace;'>slexpr<\/span><\/a><span style=' font-style: italic;'> function. This triggered a lot of questions about the impact on the code generated from a model using <\/span><a href = \"https:\/\/www.mathworks.com\/products\/embedded-coder.html\"><span style=' font-style: italic;'>Embedded Coder<\/span><\/a><span style=' font-style: italic;'>&#x2122;.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span style=' font-style: italic;'>To help answering those questions, I am happy to welcome back guest blogger <\/span><a href = \"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/15583232\"><span style=' font-style: italic;'>David Balbuena<\/span><\/a><span style=' font-style: italic;'>.<\/span><\/div><h2  style = 'margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span>Default Behavior<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>My first thought when I read Guy's <\/span><a href = \"https:\/\/blogs.mathworks.com\/simulink\/2022\/01\/19\/specifying-variable-values-using-mathematical-expressions\/\"><span>recent post<\/span><\/a><span> on <\/span><a href = \"https:\/\/www.mathworks.com\/help\/simulink\/slref\/slexpr.html\"><span style=' font-family: monospace;'>slexpr<\/span><\/a><span> was \"What about code generation?!\"<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Today I will show how you can use <\/span><a href = \"https:\/\/www.mathworks.com\/products\/embedded-coder.html\"><span>Embedded Coder<\/span><\/a><span>&#x2122; to preserve parameter expressions in the generated code.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Let's take the part of Guy's model that uses the variable <\/span><span style=' font-family: monospace;'>area<\/span><span> (I'll explain why I am leaving out the hypotenuse later):<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/slexpr_codegen_1.png\" width = \"421\" height = \"205\" alt = \"area_model.png\" style = \"vertical-align: baseline; width: 421px; height: 205px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Where <\/span><span style=' font-family: monospace;'>area<\/span><span> is created using this code:<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 0 10px 0;\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(191, 191, 191); border-right: 1px solid rgb(191, 191, 191); border-top: 1px solid rgb(191, 191, 191); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >a = Simulink.Parameter(3);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(191, 191, 191); border-right: 1px solid rgb(191, 191, 191); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >b = Simulink.Parameter(4);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(191, 191, 191); border-right: 1px solid rgb(191, 191, 191); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(191, 191, 191); border-radius: 0px 0px 4px 4px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >area = Simulink.Parameter(slexpr(<\/span><span style=\"color: rgb(167, 9, 245);\">\"(a*b)*0.5\"<\/span><span >));<\/span><\/span><\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>If you generate code using the default settings of <\/span><a href = \"http:\/\/www.mathworks.com\/help\/simulink\/slref\/simulink.parameter.html\"><span>Simulink.Parameter<\/span><\/a><span> objects, the evaluated value of <\/span><span style=' font-family: monospace;'>area<\/span><span> gets inlined:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/slexpr_codegen_2.png\" width = \"430\" height = \"260\" alt = \"nontunable_code.png\" style = \"vertical-align: baseline; width: 430px; height: 260px;\"><\/img><\/div><h2  style = 'margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span>Specifying Storage Classes to Preserve Expressions<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>It is possible for Embedded Coder to preserve the mathematical expression in the generated code by making two changes:<\/span><\/div><ol  style = 'margin: 10px 0px 20px; padding-left: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px; '><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span>For <\/span><span style=' font-family: monospace;'>a<\/span><span> and <\/span><span style=' font-family: monospace;'>b<\/span><span> change the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2022a\/ecoder\/ug\/choose-a-built-in-storage-class-for-controlling-data-representation-in-the-generated-code.html\"><span>storage class<\/span><\/a><span> to <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2022a\/ecoder\/ug\/choose-a-built-in-storage-class-for-controlling-data-representation-in-the-generated-code.html#mw_010d4edd-6f58-4343-a780-8f5e5bef2787\"><span style=' font-family: monospace;'>Define<\/span><\/a><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span>For <\/span><span style=' font-family: monospace;'>area<\/span><span>, set the storage class to <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2022a\/ecoder\/ug\/choose-a-built-in-storage-class-for-controlling-data-representation-in-the-generated-code.html#mw_fe6d6549-bf8f-40bb-a410-4dd9b19f0dc3\"><span style=' font-family: monospace;'>ExportedGlobal<\/span><\/a><\/li><\/ol><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>I like to use the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2022a\/ecoder\/ref\/codemappingseditorc.html\"><span>Code Mappings Editor<\/span><\/a><span> (ctrl+shift+C) for that, since I can mass-edit code generation settings with ctrl+click:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/slexpr_codegen_3.gif\" width = \"496\" height = \"624\" alt = \"tempgif.gif\" style = \"vertical-align: baseline; width: 496px; height: 624px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Then in the code, <\/span><span style=' font-family: monospace;'>area<\/span><span> will show up as a tunable parameter:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/slexpr_codegen_4.png\" width = \"418\" height = \"261\" alt = \"tunable_code.png\" style = \"vertical-align: baseline; width: 418px; height: 261px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>And the definition for <\/span><span style=' font-family: monospace;'>area<\/span><span> contains the mathematical expression:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/slexpr_codegen_5.png\" width = \"351\" height = \"152\" alt = \"area_definition.png\" style = \"vertical-align: baseline; width: 351px; height: 152px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>Because I used the <\/span><span style=' font-style: italic;'>Define<\/span><span> storage class for <\/span><span style=' font-family: monospace;'>a<\/span><span> and <\/span><span style=' font-family: monospace;'>b<\/span><span>, those show up as macros in the model header file:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"https:\/\/blogs.mathworks.com\/simulink\/files\/slexpr_codegen_6.png\" width = \"507\" height = \"258\" alt = \"expression_params_definition.png\" style = \"vertical-align: baseline; width: 507px; height: 258px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>These are not the only storage classes you can use. The general rule is:<\/span><\/div><ul  style = 'margin: 10px 0px 20px; padding-left: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 14px; '><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span>For parameters used in the expression (<\/span><span style=' font-family: monospace;'>a<\/span><span> and <\/span><span style=' font-family: monospace;'>b<\/span><span>), the storage class has to generate a macro, such as <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2022a\/ecoder\/ug\/choose-a-built-in-storage-class-for-controlling-data-representation-in-the-generated-code.html#mw_010d4edd-6f58-4343-a780-8f5e5bef2787\"><span style=' font-family: monospace;'>Define<\/span><\/a><span style=' font-family: monospace;'> and <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2022a\/ecoder\/ug\/choose-a-built-in-storage-class-for-controlling-data-representation-in-the-generated-code.html#mw_010d4edd-6f58-4343-a780-8f5e5bef2787\"><span style=' font-family: monospace;'>ImportedDefine<\/span><\/a><span> <\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span>For the derived parameter (<\/span><span style=' font-family: monospace;'>area<\/span><span>), storage class must be non-Auto (for example <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2022a\/ecoder\/ug\/choose-a-built-in-storage-class-for-controlling-data-representation-in-the-generated-code.html#mw_fe6d6549-bf8f-40bb-a410-4dd9b19f0dc3\"><span>ExportedGlobal<\/span><\/a><span> or <\/span><a href = \"https:\/\/www.mathworks.com\/help\/releases\/R2022a\/ecoder\/ug\/choose-a-built-in-storage-class-for-controlling-data-representation-in-the-generated-code.html#mw_c20cbc69-9433-4e68-935d-40becbc82d4d\"><span>ExportToFile<\/span><\/a><span>)<\/span><\/li><\/ul><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>In my example, I left out the hypotenuse (<\/span><span style=' font-family: monospace;'>sqrt(a^2+b^2))<\/span><span> parameter because only certain mathematical expressions are supported, and the calculation for the hypotenuse has two unsupported parts: the <\/span><span style=' font-family: monospace;'>sqrt()<\/span><span> function and the <\/span><span style=' font-family: monospace;'>^ <\/span><span>operator<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>You can read more of the details and limitations in the <\/span><a href = \"https:\/\/www.mathworks.com\/help\/ecoder\/ug\/code-generation-of-parameter-objects-with-expression-values.html\"><span>documentation<\/span><\/a><span>.<\/span><\/div><h2  style = 'margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span>Now it's your turn<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span>We are aware that this feature comes with many <\/span><a href = \"https:\/\/www.mathworks.com\/help\/ecoder\/ug\/code-generation-of-parameter-objects-with-expression-values.html#mw_4605d101-8e89-4d0e-b9a7-b2fabc7df0d1\"><span>limitations<\/span><\/a><span> and supports only a <\/span><a href = \"https:\/\/www.mathworks.com\/help\/ecoder\/ug\/code-generation-of-parameter-objects-with-expression-values.html#mw_cb8cee1a-054a-4fec-bbb6-efcf68176cd2\"><span>limited set of operations<\/span><\/a><span>. We are considering adding support for more operations and removing some of those limitations. If you are interested by this feature but are impacted by a limitation, let us know in the comments below to help us prioritize.<\/span><\/div>\r\n<\/div><script type=\"text\/javascript\">var css = ''; var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style'); head.appendChild(style); style.type = 'text\/css'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); }<\/script><a href=\"https:\/\/blogs.mathworks.com\/simulink\/files\/slexpr_codegen.mlx\"><button class=\"btn btn-sm btn_color_blue pull-right add_margin_10\">Download Live Script<\/button><\/a>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/simulink\/files\/slexpr_codegen_5.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>Some time ago, I published a post where I described how to specify variable values using mathematical expressions using the slexpr function. This triggered a lot of questions about the impact on the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2022\/03\/31\/specifying-variable-values-using-mathematical-expressions-generated-code\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":11821,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,69,16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/11770"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=11770"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/11770\/revisions"}],"predecessor-version":[{"id":12062,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/11770\/revisions\/12062"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media\/11821"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=11770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=11770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=11770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}