How to detect and print out the position of a NaN in a Simulink Bus

Dear Supporters,
i am currently working on a way to detect NaN´s and Inf´s in a Simulink Bus with about 50 signals combined. If a NaN or Inf is found, the signal should be given by a message in the console/output window. This was my idea (smaller Bus in this example, but it should not depend on the size of the bus.):
y = u, so the matlab function shouldn´t change the bus, but simply check it for NaNs and Infs and if found, it should print out the "position" and the error.
In this example the print out should be something like that:
"Warning: Inf found in Const2(Bus1)"
and
"Warning: NaN found in Const4(Bus1)"
My problem is that i am pretty new to the whole simulink/matlab environment and don´t quite know how to implement this, especially the code that i need to write in the matlab function is what i don´t quite know.
Help would be very appreciated, thanks in advance. (A small solution-example for my problem would be great)

8 Comments

Thanks for your answer, but that doesn't solve my problem. I do know how to check for NaN!
What i don't know is how to do it with a bus signal. How do i access the single elements of the bus in my matlab function?
More specific: I want to go through every single signal (one by one, maybe with a loop) and check if it is a NaN. If i find one, i want to print out the signal name.
A bus shows up as a struct at the MATLAB level. You can loop over fieldnames or use structfun()
@Walter Roberson Thanks for the answer. I found the fieldnames command but now as soon as i write "fieldnames(u)" there oocurs an error saying: "Code generation requires that the argument to FIELDNAMES be a structure."
Why is that? What is my mistake?
I am wondering if you would be able to use the field names if you knew them in advance but that you might need to go through the properties to find out what the field names are, maybe ??
@Walter Roberson I´ve already seen the first link, but i think it doesn't fit my requirements. The second one seemed to be helpful but i figured that it only helps with getting the name of the bus signal, but not the single signals that the bus includes.
Is there a function to get the amount of elements that a structure has?
If so, the last and only problem to solve is how to get the names of the signals in my bus.

Sign in to comment.

Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Products

Release

R2020b

Asked:

Tom
on 22 Jul 2022

Commented:

Tom
on 22 Jul 2022

Community Treasure Hunt

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

Start Hunting!