Using LibBlockParameter() into a conditional directive %if

13 Ansichten (letzte 30 Tage)
Nuria Bilbao
Nuria Bilbao am 11 Jun. 2012
Bearbeitet: Andrew Small am 27 Apr. 2015
Hello, I am trying to use a variable read from the S-function mask within a conditional directive, but there is no way of reading it properly! This variable is POPUP type and I wrote next linesinto my TLC but even if iDataType variable seems to be string type (if values within condition are not between quotation marks, "", code generation is erroneous due to matlab error: "The == and != operators can only be used to compare values of the same type" ) I cannot find propper values to distinguish different if cases. In the example, Code Generation is correct but iDataType has always a value out of the values I expected, because it always comes out from "else". I also tryed "1", "2","3" and same strings as the ones used in the popup definition but had the same wrong response.
%assign iDataType =LibBlockParameter(DataType, "", "", 0)
%if iDataType == "1.0"
...
%elseif iDataType == "2.0"
...
%elseif iDataType== "3.0"
...
%else
...
%endif
I hope somebody can help me, and thank you in advance! Nuria

Akzeptierte Antwort

Kaustubha Govind
Kaustubha Govind am 11 Jun. 2012
I'm wondering if the problem is that you need to do string comparison instead of using the == operator. Since I can't think of a way to do string comparison in TLC, I will suggest using FEVAL to call into MATLAB:
%assign iDataType =LibBlockParameter(DataType, "", "", 0)
%assign isOne = FEVAL("strcmp", iDataType, "1.0")
%assign isTwo = FEVAL("strcmp", iDataType, "2.0")
%assign isThree = FEVAL("strcmp", iDataType, "3.0")
%if isOne
...
%elseif isTwo
...
%elseif isThree
...
%else
...
%endif
  3 Kommentare
Kaustubha Govind
Kaustubha Govind am 19 Jun. 2012
Nuria: Could you accept my answer if it helped you? Thanks!
Nuria Bilbao
Nuria Bilbao am 20 Jun. 2012
Done.
Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrew Small
Andrew Small am 27 Apr. 2015
Bearbeitet: Andrew Small am 27 Apr. 2015
I needed something similar, selecting alternative C code depending on the selection in a Popup. The error hander will stop the build if the popup's value is out of bounds (should never be, unless the mask design changes).
  • p1 was the intrinsic name of the block's mask parameter list, no %assign within the TLC file.
  • the queried popup was the first item in the list the index was 0.
%function Outputs(block, system) Output
...
%assign r_unit = LibBlockParameterValue(p1,0)
%switch(r_unit)
%case 1
...
%break
%case 2
...
%break
%default
%<LibBlockReportError(block, "Unrecognised Unit selection")>
%break
%endswitch
...
%endfunction

Kategorien

Mehr zu Simulink Coder finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by