how to change name of simulink blocks through matlab code

19 次查看(过去 30 天)
exl = actxserver('excel.application');
exlWkbk = exl.workbooks.Add;
new1= readtable('names.xlsx');
list_of_block_in_subsystem = find_system('Auto_Script','type', 'block');
for i = 1:length(list_of_block_in_subsystem)
old_name = get_param(list_of_block_in_subsystem{i},'Name');
new_name = new1.aa{i};
set_param(list_of_block_in_subsystem{i}, 'Name', new_name);
end
%code is not running completely only half of the names changes and also code is executing only ones.

回答(1 个)

T.Nikhil kumar
T.Nikhil kumar 2024-2-14
Hello Vibhu,
I get that you are trying to rename the blocks in your Simulink model using a MATLAB script. Based on your code, here are a few observations which could be reasons why your MATLAB code is not working correctly:
  1. When you use the ‘readtable’ command to read from ‘.xlsx’ file, you should set the ‘ReadRowNames’ property to false when the first column of the region to read contains data, not the row names for the table. Your model contains 27 blocks and the excel sheet contains 27 corresponding new names. You must use ‘readtable’ with this property set for correct mapping of new names for blocks.
  2. Also, you need to keep in mind that renaming a block can potentially break links to this block if other blocks or scripts reference it by its old name. Ensure that you update all references accordingly.
  3. You are creating an Excel server but not using it in the code you provided. If you intend only to read from an Excel file, you can use ‘readtable’ directly without creating an Excel server.
Refer to the following documentation to understand about 'ReadRowNames' property of 'readtable' function:
Hope this helps you proceed further!

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by