How to output text to Command Window if signal changes from 0 to 1 in Simulink

2 visualizaciones (últimos 30 días)
I want to output text to the command line in Simulink when a signal turns from 0 to 1.
For example, in my model, signal A is zero unless a condition is exceeded in the model, to where signal A is then turned to one. At that point, I want to write "Signal A is 1. Data is invalid" to the command window. I'm currently trying to use a MATLAB function block:
function a = fcn (u)
if u == 1
disp('Signal A is 1. Data is invalid')
a = 1;
else
end
This doesn't seem to work. any suggestions?

Respuestas (1)

Fangjun Jiang
Fangjun Jiang el 24 de En. de 2025
You might want to do that, but MATLAB and Simulink are quite different.
I don't think you can do that because disp() or fprintf(), etc are not supported.
MATLAB System block can output free style text, but to the Simulink Diagnostic Window, not to the MATLAB Command Window.
  6 comentarios
Paul
Paul el 27 de En. de 2025
I'm quite surprised that disp doesn't work that way in 2022b. My vague recollection is that disp has worked that way for quite some time; I only checked it in 2024a to verify my memory.
This doc page talks about using only supported functions in a Matlab Function block.
"Because the MATLAB Function block relies on code generation technology to compile and simulate, C/C++ code generation limitations for MATLAB Coder also apply to MATLAB Function blocks. As a result, you can only use MATLAB language features that are [NOT] supported for C/C++ code generation in MATLAB Function block code if you declare them as extrinsic."
I added what I think is the appropriate correction.
Or maybe they meant to say: "As a result, you can only use MATLAB language features that are supported for C/C++ code generation in MATLAB Function block code [UNLESS] you declare them as extrinsic."
But I still think that second statement isn't 100% correct as there are some functions, like plot() for example IIRC, that will work even if not declared extrinsic.
That page has a link to this doc page from which the full list of functions that support code generatation is accessible.
Fangjun Jiang
Fangjun Jiang el 28 de En. de 2025
@Paul, @Ian, the disp() messages are shown in Simulink Diagnostic window, verified in R2022b, R2024b and assumed others too.
Previously, my experiments in R2022b didn't show the message. That is because the "if u == 1" logic. It's better to change it to "if u >= 1"

Iniciar sesión para comentar.

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by