How to Control Simulink from Matlab during Simulation

2 Ansichten (letzte 30 Tage)
DJ V
DJ V am 9 Jul. 2025
Kommentiert: DJ V am 14 Jul. 2025
I have written a control scheme using Simulink. I need to control it using Matlab. I am currently using fixed values for variables such as the degree of deflection of aerodynamic control surfaces. I need to be able to change the values of those variable as I am flying the aircraft (running Simulink simulation). (dA might be change of Aeleron. dR might be change of rudder. Etc.) How do I make these dX values variable? How do I replace the fixed dX values in the program with variables that can be changed? How do I make the dX values variable? This includes throttle settings for speed that may change. Can you please asnwer my question specifically? I really need a usable answer. It would be helpful if I could simply use left and right arrow keys to control motion left or right and the up down arrow keys to control moiton that is up or down. Maybe F and S keys for faster and slower? Some means of displaying how far I'm driving the variable toward its max or min value on screen would also be very useful.

Antworten (2)

Sam Chak
Sam Chak am 11 Jul. 2025
I am unable to address all questions in a single post, as some require knowledge of aerospace engineering. However, to achieve the functionality of increasing and decreasing the value of a variable x in MATLAB based on key presses, you may consider the following approach:
%% create a function file for "keyPressExample.m"
function keyPressExample()
% Initialize variable
x = 0; % Initial value
increment = 1; % Amount to change x
isFPressed = false; % Flag for F key
isSPressed = false; % Flag for S key
% Create a fig
fig = figure('KeyPressFcn', @keyPress, 'KeyReleaseFcn', @keyRelease);
% Start a timer to update x
t = timer('ExecutionMode', 'fixedRate', 'Period', 0.1, 'TimerFcn', @(~,~) updateX());
start(t);
% Nested function to handle key press
function keyPress(~, event)
switch event.Key
case 'f'
isFPressed = true;
case 's'
isSPressed = true;
end
end
% Nested function to handle key release
function keyRelease(~, event)
switch event.Key
case 'f'
isFPressed = false;
case 's'
isSPressed = false;
end
end
% Nested function to update x
function updateX()
if isFPressed
x = x + increment; % Increase x
elseif isSPressed
x = x - increment; % Decrease x
end
disp(x); % Display the current value of x
end
% Clean up when figure is closed
set(fig, 'CloseRequestFcn', @(src, event) closeFigure(src, t));
end
%% create a function file for "closeFigure.m" (Click the "×" button to close)
function closeFigure(fig, t)
stop(t);
delete(t);
delete(fig);
end
  1 Kommentar
DJ V
DJ V am 14 Jul. 2025
Okay, why are you opening and closing a figure? I merely need to transmit data to simulink. How do I establish when I connect to simulink to transmit variable value updates? How do I establish when Simuink is done so I can collect the results, or do I merely update periodically? How do I select linking variables for the variables that I'm changing in simulink? In other words, how do I choose the block or variable type to accept the data I'm sending simulink? I suppose the block opening and closing is because I need to update the simulation in matlab (the animation) as it runs. How do I periodically accomplish this? Another timer? Keep in mind that I need to download data from Simulink to update the Matlab screen. Do I pause simulink while doing that?

Melden Sie sich an, um zu kommentieren.


Paul
Paul am 12 Jul. 2025
Perhaps dashboard blocks would be helpful.

Kategorien

Mehr zu Simulation finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by