Using array of strings in TLC files

조회 수: 5 (최근 30일)
Macko
Macko 2011년 7월 11일
Hi,
I'm trying to use an array of predefined strings in a TLC file and index this array using an input parameter.
Let's say I have the following array of strings:
names = ["Jane" "John" "Jeff"]
I'd like use something like the following:
myFunction(%<names[LibBlockInputSignal(0, "", "", 1)]>, 100)
which should output myFunction("John", 100);
Any idea how to do that? Mainly, how to build up the array and select/echo the desired string?
Many thanks in advance!
  댓글 수: 1
Oleg Komarov
Oleg Komarov 2011년 7월 11일
Not clear what you want to do. Which criteria you use to select the name.

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

채택된 답변

Kaustubha Govind
Kaustubha Govind 2011년 7월 11일
Try:
%assign names = ["Jane", "John", "Jeff"]
myFunction(%<names[1]>, 100)
Although it's not clear what you want to do with LibBlockInputSignal(0, "", "", 1), because this would generate code which could replace LibBlockInputSignal(0, "", "", 1) with something like mymodel_U.In1[0] (as an example). Therefore something like:
myFunction(%<names[LibBlockInputSignal(0, "", "", 1)]>, 100)
Would produce an error in TLC because LibBlockInputSignal(0, "", "", 1) is not a TLC constant. If you really want to index into a string array based on the input value, you need to generate the string array in the code. For example:
%assign u = LibBlockInputSignal(0, "", "", 1)
char *names[] = {"Jane" "John" "Jeff"};
if (%<u> >= 0 && %<u> < 3) {
myFunction(names[%<u>], 100);
}
Full disclaimer: I haven't tested the above code in an S-function, so you might need to tweak it further.
  댓글 수: 2
Macko
Macko 2011년 7월 12일
Thanks a lot! That looks really good!
I will give this a try and let you know the outcome.
Macko
Macko 2011년 7월 14일
It works! Thank you so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by