How to determine simulink input in real time using arrow keys

15 Ansichten (letzte 30 Tage)
Kuwata
Kuwata am 20 Jan. 2024
Beantwortet: covao am 29 Jan. 2024
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?

Akzeptierte Antwort

covao
covao am 29 Jan. 2024
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. 

Weitere Antworten (0)

Kategorien

Mehr zu イベント関数 finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!