How to Control Simulink from Matlab during Simulation
    4 次查看(过去 30 天)
  
       显示 更早的评论
    
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.
0 个评论
回答(2 个)
  Sam Chak
      
      
 2025-7-11
        Hi @DJ V
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


