I want to connect the data table with matlab code. Can anyone help me with the code to generate the particular block which are mentioned in the data table first column.

1 view (last 30 days)
The first column of data table contains the object details to be created with the help of matlab code. I know the the automation code to create the object. But I am unable to link the data table witht the matlab code. Can anyone help me with the code to link the data table and matlab code? I want to use the for loop to connect the first column of data table, and then if conditions for object creation. and then with if condition how can I compare multiple machine objects and name it?
I have tried this way of code. To connect the source from table file. Everything works fine. It creates a new model but it doesnt generate a block in that model.
  7 Comments
Walter Roberson
Walter Roberson on 2 Mar 2022
Stations is a table. Stations(i,1) would be a 1 x 1 table() . I am not sure what strcmp() would do with that.
Stations{i,1} would be the character vector.

Sign in to comment.

Answers (1)

Ravi
Ravi on 2 Feb 2024
Hi Andanu Vidya Sagar Patro,
Accessing the entries in a table should be done using curly brace notation. In addition to that, when using the “add_block” function, the source argument must be the absolute path of the library block. Here, Entity Generator, Entity Server, and Entity Queue, though they are present in the “SimEvents” package, they should be referenced using “sldelib”.
It can be observed from the following.
  1. Open the model.
  2. Open Library Browser and search for the required block.
  3. Place the pointer on the block, and you will see a tooltip denoting the absolute path of the block.
number_of_blocks = 7;
for i = 1:number_of_blocks
objectType = data{i, 2};
name = data{i, 1};
x = data{i, 3};
y = data{i, 4};
u = data{i, 5};
v = data{i, 6};
pt = data{i, 7};
cap = data{i, 8};
handle = [];
destinationBlockPath = "SimEventsModel/" + name;
if strcmp(objectType, 'Entity Generator')
handle = add_block('sldelib/Entity Generator', destinationBlockPath);
elseif strcmp(objectType, 'Server')
handle = add_block('sldelib/Entity Server', destinationBlockPath);
elseif strcmp(objectType, 'Entity Queue')
handle = add_block('sldelib/Entity Queue', destinationBlockPath);
elseif strcmp(objectType, 'Sink')
handle = add_block('simulink/Sinks/Scope', destinationBlockPath);
end
end
Here, assume “data” to be the table. We can create a block using “add_block” function and set parameters to a block using “set_param” function. Once after we create the block, we can add parameters to it as follows, by checking if it is valid firstly,
if ~isnan(cap)
set_param(destinationBlockPath, "Capacity", num2str(cap));
end
Since, the sink you have mentioned could not be found in the SimEvents library, I have used a Scope block instead.
To learn more about the functions used, please refer to the following resources.
I hope this answer resolves the issue you are facing.
Thanks,
Ravi

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!