Neglection of [outport]variable generated for bus assignment block

My intention is to generate th code as mentioned below
void computeArea() {
rectangle.area = rectangle.length * rectangle.width;
}

1 Comment

Kindly let me know the solution as soon as possible.
I am hopeless!!!

Sign in to comment.

Answers (2)

Hi Gowtham,
As per the image you attached and some guesswork, this could be because you're using virtual buses throughout the model. Virtual buses are just an graphical affordance to avoid cluttering the model and make it look cleaner. However, internally the member variables are considered as separate entities. You will have to use a non-virtual bus which actually treats the different variables as a single entity. You may refer the following documentations for more information:
Thanks
Akshat
hello @Gowtham,
As I do not have the simulink model, I tried to make a simple simulink model to reproduce the issue as shown below. I am also attaching it with this answer.
I was able to generate code that was of format:
void computeArea() {
rectangle.area = rectangle.length * rectangle.width;
}
You just need to do some optimisation setting under "Model Configurations -> Code Generation -> Optimisation:". I have done the following optimisation settings:
CASE-1: buid code for Generic real time (.grt)
below given are the optimisation settings:
The screen-shot of generated code is:
CASE-2: Build code for embedded real time (.ert)
below given are the optimisation settings:
The screenshot of generated code is:
I hope you find this useful !

6 Comments

What you have created is inporting elements from Simulink bus,computation and saving values in outport structure.
But my requirement is to collect elements of Simulink bus and after computation need to storee in same onpi inport instead of storing it in ouport varuable in generated c.
Knindly guide me out
hello @Gowtham,
Thanks for sharing the files. I am not able to reproduce the issue from my end. I run the file "in_out_struct" attached by you in the comment and also loaded the workspace by running "in_out_struct_data.m".
For generating code, I selected "Embedded coder" and it generated the code whose screen shot I am attaching below:
It seems that it is aligned with what you are expecting.
Which code did you use?
Simulink coder or embedded coder?
Try using embedded coder (ert.tlc)
As you mentioned in screenshot I also generated code as you mentioned .But the problem about [out1] variable generation in code which is unnecessary because my intention is to do multiplication of length and width from inport bus object (i.e: Rectangle )and store it has Area in same input bus object.(i.e: Rectangle )
For example :
(In executable code you can consider below code)
#include <stdio.h>
// Define the structure
typedef struct {
float length;
float width;
float area;
}; struct_alias
//global declaration
struct_alias Rectangle
void computeArea() {
Rectangle.area = Rectangle.length * Rectangle.width;
}
int main() {
// Initialize structure
Rectangle.length= 5;// consider it as value got from inport block
Rectangle.width=7;// consider it as value got from inport block
// Call function and update structure
computeArea(Rectangle);
// Print updated area
printf("Updated Area: %.2f\n", Rectangle.area);/ consider it as value got from inport block
return 0;
}
Please let know is there any possiblity to get intended code as output
Hello @Gowtham,
The simulink model contains inport "Rectangle" and outport "out1". Therefore, the generated code has two global variables: "Rectange" and "out1". The datatypes of both of them are "Bus object" (Struct_Alias).
Notice that when you enter the calculare_Area() function, the value of bus "Rectangle" gets stored in "out1" and then the "Area" object of "out1" gets the value of "Area" .
Therefore, it seems to be an expected behaviour.

Dear @Shivam Gothi Thank you for your response.I accept that the "out1"is created as object because of model design. But in order to get my intended code as multiplication of Rectangle.width and Rectangle. Length and storing in same bus object as Rectangle.area . How the model design need to be. Kindly help me out

As per the requirement creation of out variable and assigning it as object is unnecessary.

Sign in to comment.

Asked:

on 29 Jan 2025

Commented:

on 31 Jan 2025

Community Treasure Hunt

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

Start Hunting!