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 Center および File Exchangeプログラムによるモデル編集 についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!