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 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/911635/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/911695/image.png)
7 Kommentare
Walter Roberson
am 2 Mär. 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.
Antworten (1)
Ravi
am 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.
- Open the model.
- Open Library Browser and search for the required block.
- Place the pointer on the block, and you will see a tooltip denoting the absolute path of the block.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1606621/image.png)
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Simulink Functions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!