Link Simulink Signal to App Designer (Lamp)

8 次查看(过去 30 天)
Hello MatLab users,
I want to create an App that can control Inputs to my Simulink model and display signal values/status during simulation. PLEASE SEE ATTACHMENTS
Using App Designer and the Common Components - for most components you can create Callbacks - creates function NAME(app,event). When componet value changes - perform action. In my attachments - when the user changes the 'Ignition_Status' knob - the 'Ignition_Status' simulink block value changes.
function Ignition_StatusValueChanged(app,event)
value = app.Ignition_Status.Value;
if strcmp(value, 'Off')
% If Ignition_Status knob is changed to OFF --> Simulink 'Ignition_Status' Signal = 0
set_param('GUI_Interactive_Model/Ignition_Status','Value', '0');
elseif strcmp(value, 'Acc')
% If Ignition_Status knob is changed to Acc --> Simulink 'Ignition_Status' Signal = 2
set_param('GUI_Interactive_Model/Ignition_Status','Value', '2');
elseif strcmp(value, 'Run')
% If Ignition_Status knob is changed to Run --> Simulink 'Ignition_Status' Signal = 4
set_param('GUI_Interactive_Model/Ignition_Status','Value', '4');
elseif strcmp(value, 'Crank')
% If Ignition_Status knob is changed to Crank --> Simulink 'Ignition_Status' Signal = 8
set_param('GUI_Interactive_Model/Ignition_Status','Value', '8');
end
end
function func_lamp_status(app,Model)
% CODE DOES NOT WORK -- BUT I WANT TO PERFORM THIS ACTION -- PLEASE ADVISE!!!
% CONSTANTLY MONITOR SIGNAL -- OR -- APP IS NOTIFIED THAT Simulink Signal 'Power_Value' has CHANGED
value = get_param(['GUI_Interactive_Model/Power_Value'],'Value');
if value == 1
app.Lamp.Enable = 'on'; % TURN App LAMP ON
elseif value == 0
app.Lamp.Enable = 'off'; % TURN App LAMP OFF
end
end
end
In the Simulink Model - Output of the stateflow is the signal Power. In Simulink - I added a 'Lamp'. In "Block Parameters: Lamp" you can set the Color of the Lamp based on the State of a signal. When I run the simulation the Simulink Lamp correctly changes color based on the Output of the stateflow.
**** HOW CAN I LINK THE STATE of the Simulink Signal --> to app.Lamp.Enable 'on' or 'off'?

回答(2 个)

Nathan Gyger
Nathan Gyger 2019-10-9
Hello Michael,
In order to have a periodical update of the UI, it is possible to create a timer object that is connected to a callback-function. The update interval can be set and then the specified callback-function is executed at this rate. Inside this function the status of the LED can be read with the get_param() command.
The timer class is explained here: https://mathworks.com/help/matlab/ref/timer-class.html
In addition, MathWorks as a nice example app that shows the concept. This app can be opened directly in MATLAB with the following command:
>> openExample('matlab/AppdMemoryMonitorExample')
I hope that this is helpful.
  1 个评论
Michael Jarboe
Michael Jarboe 2019-10-10
I will give this a try, thank you for the input! hopefully I can get the output :)

请先登录,再进行评论。


Hammad
Hammad 2024-12-15

% Signal Parameters Fs_original = 320000; % Original signal frequency (Hz) T_original = 1/Fs_original; % Original signal sampling period t_original = 0:T_original:0.002; % Time vector for 2 ms

% Original Signal x_original = cos(2*pi*5000*t_original) + sin(2*pi*10000*t_original);

% Plotting the Original Signal figure; subplot(3, 1, 1); plot(t_original, x_original, 'b', 'LineWidth', 2); title('Original Signal'); xlabel('Time (s)'); ylabel('Amplitude');

% Nyquist Frequency Fn_original = Fs_original / 2;

% Sampling Frequencies Fs_sampled = [320000, 160000, 80000, 40000, 20000]; % Sampling frequencies (Hz) for i = 1:length(Fs_sampled)

    % Nyquist-Shannon Sampling Theorem Check  
    if Fs_sampled(i) >= 2 * Fn_original  
        disp(['Sampling at Fs = ' num2str(Fs_sampled(i)/1000) ' kHz is valid (Nyquist criteria satisfied).']);  
        % Sampling  
        T_sampled = 1/Fs_sampled(i);  
        t_sampled = 0:T_sampled:0.002;  
        x_sampled = cos(2*pi*5000*t_sampled) + sin(2*pi*10000*t_sampled);  
        % Reconstruction using zero-order hold  
        x_reconstructed = zeros(size(t_original));  
        for j = 1:length(t_sampled)  
            % Find the nearest index and assign the sampled value
            [~, idx] = min(abs(t_original - t_sampled(j)));  
            x_reconstructed(idx) = x_sampled(j);  
        end  
        % Plotting the Sampled Signal  
        figure;  
        subplot(2, 1, 1);  
        stem(t_sampled, x_sampled, 'r', 'LineWidth', 2);  
        title(['Sampled Signal at Fs = ' num2str(Fs_sampled(i)/1000) ' kHz']);  
        xlabel('Time (s)');  
        ylabel('Amplitude');  
        % Plotting the Reconstructed Signal  
        subplot(2, 1, 2);  
        plot(t_original, x_original, 'b', 'LineWidth', 2);  
        hold on;  
        plot(t_original, x_reconstructed, 'g', 'LineWidth', 2);  
        title(['Reconstructed Signal at Fs = ' num2str(Fs_sampled(i)/1000) ' kHz']);  
        xlabel('Time (s)');  
        ylabel('Amplitude');  
        legend('Original Signal', 'Reconstructed Signal');  
    else  
        disp(['Sampling at Fs = ' num2str(Fs_sampled(i)/1000) ' kHz is invalid (Nyquist criteria not satisfied).']);  
    end  
end
if true
  % code
end

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by