How to determine simulink input in real time using arrow keys
13 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (1 件)
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.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!