How to determine simulink input in real time using arrow keys

조회 수: 13 (최근 30일)
Kuwata
Kuwata 2024년 1월 20일
답변: covao 2024년 1월 29일
When executing a Simulink model, I want the output to be pi/8 while the up arrow key is pressed, -pi/8 while the down arrow key is pressed, and zero otherwise (when both arrow keys are released).
I'd like to detect key inputs in real-time during simulation, and update the output at each simulation step.
I use a MATLAB Function block in Simulink. The program for the MATLAB Function block is as follows:
function out = fnc()
% get handle to use GetKeyInput function
h = @GetKeyInput;
out = h();
end
The program for the external function, GetKeyInput.m, is as follows:
function output = GetKeyInput()
persistent outputValue;
if isempty(outputValue)
outputValue = 0; % initial
end
fig = gcf;
% get ket input
keyInput = get(fig, 'CurrentCharacter');
switch keyInput
case 30 % uparrow ASCIIcode:30
outputValue = pi/8;
case 31 % downarrow ASCIIcode:31
outputValue = -pi/8;
case 28
outputValue = 0;
end
% output
output = outputValue;
end
When running this Simulink model, I encounter the error as follow:
Cannot pass a mxArray to 'eml_switch_helper'.
Function 'GetKeyInput.m' (#29.195.203), line 13, column 8:
"keyInput"
Launch diagnostic report.
How can I get the simulation to work?

답변 (1개)

covao
covao 2024년 1월 29일
This is caused by using a MATLAB function block that is not supported by code generation.
Please refer to the following document for details.
A simple solution is to use Interpreted MATLAB Function.
Set GetKeyInput() to the block parameter. 

카테고리

Help CenterFile Exchange에서 プログラムによるモデル編集에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!