필터 지우기
필터 지우기

Using LibBlockParameter() into a conditional directive %if

조회 수: 4 (최근 30일)
Nuria Bilbao
Nuria Bilbao 2012년 6월 11일
편집: Andrew Small 2015년 4월 27일
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

채택된 답변

Kaustubha Govind
Kaustubha Govind 2012년 6월 11일
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
Kaustubha Govind
Kaustubha Govind 2012년 6월 19일
Nuria: Could you accept my answer if it helped you? Thanks!
Nuria Bilbao
Nuria Bilbao 2012년 6월 20일
Done.
Thank you!

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

추가 답변 (1개)

Andrew Small
Andrew Small 2015년 4월 27일
편집: Andrew Small 2015년 4월 27일
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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by