using add_block in a for loop

조회 수: 8 (최근 30일)
Metin
Metin 2014년 12월 3일
편집: Metin 2014년 12월 3일
Hello, my problem is about creating Simulink Model in with script, due to variable model size I want to generate my model with help of for loops. When I use such command:
for k=1:n
Submodulename =sprintf('Submodule_%d' ,k);
pos = [x y+3*k*offset (x+w) (y+h+6*k*offset)];
add_block('NPC_MMC_Library/Submodule', Submodulename,...
'Position',pos);
end
Even my variable block name Submodulename is changing but add_block doesn't work,
thanx for any help in advance, Regards
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 3일
Have you any error massage?
Metin
Metin 2014년 12월 3일
Yes I had to write it before here my error message:
Error using createNPC (line 49)
A new block named 'Submodule_1' cannot be added
Hereby cereatedNPC is the name of the my simulink model, thanks

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

채택된 답변

C.J. Harris
C.J. Harris 2014년 12월 3일
편집: C.J. Harris 2014년 12월 3일
I see you have defined a source block ('NPC_MMC_Library/Submodule'), however your destination doesn't appear to include the model name you are adding it to. I would expect your source to be of the format ['my_model/', Submodulename].
The reason it is failing is you are trying to add a block without stating which model you are adding it to.
Try this:
for k=1:n
Submodulename =sprintf('Submodule_%d' ,k);
pos = [x y+3*k*offset (x+w) (y+h+6*k*offset)];
add_block('NPC_MMC_Library/Submodule', ['my_model/', Submodulename], ...
'Position',pos);
end
Note: Replace 'my_model' with the name of your Simulink model
  댓글 수: 1
Metin
Metin 2014년 12월 3일
편집: Metin 2014년 12월 3일
thanks for the answer it works well:)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by