How to use integer or other datatype condition in if Block, Simulink?

Hello,
I want to use condition like if (u ==1), IF block has single/double data type by default, so whenever I generate the code from simulink model, this condition shows u == 1.0F How can I change data type to Int or Boolean so in the autocode it will show u == 1.0 ??
Or any other way to implement if elseif condition where I could change the data type of if condition in Simulink?
Thanks in advance for your answer.

 Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 24 Mär. 2024

0 Stimmen

Try this, Use a Relational Operator block where you can specify any data type for the inputs. Specify the output data type as boolean which is the default. Feed this to the IF block where the condition is if (u)

12 Kommentare

Thanks for the feedback. But I have this condition to implement in if statement (u1 < 0) | (u3 ~= 1)... how is that possible with relational operator as I have total 3 inputs to if block?
Could I use stateflow for this implementation ?
Implement the (u1 < 0) | (u3 ~= 1)... logic using Relational Operator and Logical Operator, then feed the final boolean output to the IF block.
Stateflow could be used but I would say it is an overkill.
There is a problem. For u1 input I have float data type and for u3 I have int8 data type. Now if I use relational operators here, how should I feed to IF block, what should I write in the IF block statement ?
Use Relational Operator blocks to do those individual comparison, then use the Logical Operator to combine them and then feed the final boolean output to the IF block.
I see your point. I can directly put the input coming to IF block from logical operator output. No need any conditions inside the IF block.
Thanks.
one more concern, if I put just u1 here in the If expression, what should it take the value? like if u1 input(coming from that logical operator) is 0 means not executing the IF subsystem block, and u1 input to this IF block is 1, then it will execute first IF action subsystem output?
Yes.
When "flag" is a boolean type, then
if (flag)
is the same as
if (flag==true)
thanks for the feedback.
But actually it's not boolean, I mean when I run it with this implementation, Simulink gives me error saying I need single/double(datatype) input to IF block, so I putted data type conversion block to conver the output of logical block into Single data type and feed it to IF block. Which run successfully. That means it behave the same way as you explain right?
Try it again. I tried. Make the "If expression" as u1. It accepts boolean type.
However, in the document, it says
"The If block does not support a boolean data type. If you want to input a boolean signal to an If block, convert the signal to an integer type using the Data Type Conversion block."
I see, yes it's runnig now without errors.
Thanks for the help
Update:
I reported the incorrect documentation to the Mathworks tech support. I have been notified that an update/correction of the document will be coming soon.
That's great, thanks for the update.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2020b

Gefragt:

am 22 Mär. 2024

Kommentiert:

am 27 Mär. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by