How to determine simulink input in real time using arrow keys

15 vues (au cours des 30 derniers jours)
Kuwata
Kuwata le 20 Jan 2024
Réponse apportée : covao le 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?

Réponse acceptée

covao
covao le 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. 

Plus de réponses (0)

Catégories

En savoir plus sur イベント関数 dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!