problem with automized add_block destinations

hello,
I am using the add_block function to automatize the creation of a model in simulink. I have stored the block names in a column of a table data, i would like to add the blocks and automatically change their names according to the string data of the table. Here is the code:
nL = numel(data.X)
for i=1:nl
h=add_block('MyLibrary/BlockName' , ['MyModel/',data.Names(i)])
end
here is the error message:
"Invalid destination block specification."
data.Names is a string column of the table data. I have tried using another column of the table
for i=1:nl
h=add_block('MyLibrary/BlockName' , ['MyModel/',num2str(data.X(i))])
end
and this works perfectly! What i am missing? please help! Thank you all!

4 件のコメント

Ameer Hamza
Ameer Hamza 2020 年 5 月 8 日
編集済み: Ameer Hamza 2020 年 5 月 8 日
Can you paste the output of
data.Names(1)
Davide Maglione
Davide Maglione 2020 年 5 月 8 日
ans =
"BN3N_PDLN"
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 5 月 8 日
you are accessing to your stored created block names in strings via their index numbers. That is it.
Davide Maglione
Davide Maglione 2020 年 5 月 8 日
ok, how can i fix this problem? thank you for answering me

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 5 月 8 日
編集済み: Ameer Hamza 2020 年 5 月 8 日

1 投票

Change the line to this
h=add_block('MyLibrary/BlockName' , ['MyModel/',char(data.Names(i))])
%^ add char here

6 件のコメント

Davide Maglione
Davide Maglione 2020 年 5 月 8 日
thank you very much Ameer!
Ameer Hamza
Ameer Hamza 2020 年 5 月 8 日
I am glad to be of help.
The actual reason is that you were trying to use char() and string() datatype in the same vector. These are distinct data types in MATLAB and not interchangeable. The code in answer makes both variables of type 'char', which corrects the issue.
Davide Maglione
Davide Maglione 2020 年 5 月 9 日
ok thank you for the explanation. doesn't the "num2str" function transform a number into a string? why that one was working and the use of an actual string data not?
Ameer Hamza
Ameer Hamza 2020 年 5 月 9 日
The name is a bit misleading, but there is a good reason. Traditionally, MATLAB only used 'char' data type, and the terms char array and strings were used interchangeably. However, from R2016b, MATLAB officially introduced "string" data type. The function num2str existed since R2006a, and it outputs a char array. Historically the name num2str makes sense. But after the introduction of a separate "string" data type, this becomes a bit misleading. To maintain backward compatibility, the function still output char array.
Davide Maglione
Davide Maglione 2020 年 5 月 9 日
now is all clear, thank you very much
Ameer Hamza
Ameer Hamza 2020 年 5 月 9 日
I am glad to be of help.

サインインしてコメントする。

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020 年 5 月 8 日

0 投票

you are accessing to your stored created block names in strings via their index numbers. That is it.

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by