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

조회 수: 3 (최근 30일)
Ian
Ian 2025년 1월 24일
댓글: Fangjun Jiang 2025년 1월 28일
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?

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2025년 1월 24일
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
Paul
Paul 2025년 1월 27일
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 2025년 1월 28일
@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"

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

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by