I am trying to create a function that can create a block table that contain their Handle, FullName, BlockType, Parent by using struct. However each time I get enter a model name, it said
Error in createBlockTable (line 9)
tableStruct = struct(get(gcb), 'Handle', get(i), 'FullName');
Here is my code
function blockTable = createBlockTable(modelName)
load_system(modelName);
getBlock = find_system(modelName, 'FindAll', 'on', 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Type', 'Block');
blockCell = cell(1,length(getBlock));
for n = 1:length(getBlock)
for i = getBlock(n, 1)
tableStruct = struct(get(i), 'Name');
end
end
blockTable = assign(tableStruct, blockCell);
end
I am thinking that I mess up at line i = getBlock(:,1), but I do not know how to solve this problem. Thanks

댓글 수: 2

Is i a vector? What does it show if you simply do this
i = getBlock(:,1)
whos i
Maybe if i is a single number you want to do this:
for n = 1:length(getBlock)
gb = getBlock(n, 1)
tableStruct = struct(get(gb), 'Name');
end
but I don't have Simulink so that's just a wild guess.
Khoi Le
Khoi Le 2020년 11월 30일
i here is to get the block in the model, I am trying to get the name of each block so I can put it on a structure. but really do not understand how to get it right

댓글을 달려면 로그인하십시오.

 채택된 답변

Khoi Le
Khoi Le 2020년 12월 1일

1 개 추천

I have solve the problem, thanks to @Fangjun Jiang that gave me some idea
copyModel = 'sldemo_fuelsys_copy';
blockTable = makeTable(copyModel);
save('blockTable.mat','blockTable');
function blockTable = makeTable(modelName)
load_system(modelName);
getBlock = find_system(modelName, 'FindAll', 'on', 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Type', 'Block');
blockCell = cell(1, length(getBlock));
for n = 1:length(getBlock)
structTable = struct('Handle', get(getBlock(n), 'Handle'), 'Name', ...
get(getBlock(n), 'Name'), ...
'Block_Type', get(getBlock(n), 'BlockType'), ...
'Parent', get(getBlock(n), 'Parent') );
blockCell{1, n} = structTable;
end
blockTable = blockCell;
end

댓글 수: 1

Fangjun Jiang
Fangjun Jiang 2020년 12월 1일
No need to use for-loop. cell2struct() can do the job as in my example.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2020년 11월 30일

0 개 추천

This inner loop does not make sense.
for i = getBlock(n, 1)
tableStruct = struct(get(i), 'Name');
end
Each block has different properties so don't expect you can use get() to create a universally consistent structure array. get() already returens a structure. It contains different field names for each block.
The proper way is to go through a loop, get the value for "Handle, FullName, BlockType, Parent" and then construct the structure or table.
You could also do get(getBlock,'Handle') or get(getBlock,'Name') to get the property values for all the blocks at once, but to do get(getBlock,'BlockType') or get(getBlock,'Parent'), you need to exclude the model itself.

댓글 수: 4

Khoi Le
Khoi Le 2020년 11월 30일
but how to get every block in the model so the output can be on a struct. I am messing up with i = getBlock(n,1)
since it only get the last number of the length not all of it, like I have 244 block, it only get the 244 not from the first to the last
%%
modelName='f14';
open_system(modelName);
getBlock = find_system(modelName, 'FindAll', 'on', 'FollowLinks', 'on', 'LookUnderMasks', 'all', 'Type', 'Block');
Names=get(getBlock,'Name');
Parents=get(getBlock,'Parent');
blockTable=table(Names,Parents);
Khoi Le
Khoi Le 2020년 12월 1일
wow, nice support, but I have other question, is there anyway can but this table to a cell and each cell have a struct which contain Name, Parent and Handle of each block on a cell like 1x244
Fangjun Jiang
Fangjun Jiang 2020년 12월 1일
편집: Fangjun Jiang 2020년 12월 1일
blockStruct=cell2struct([Names,Parents],{'Names','Parents'},2)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

제품

질문:

2020년 11월 30일

댓글:

2020년 12월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by