Slider in Matlab code

12 次查看(过去 30 天)
Yamina chbak
Yamina chbak 2024-1-7
评论: Voss 2024-1-10
MATLAB code is a basic interactive application for visualizing a stochastic process :
close all
clc
% Initialize figure
fig = figure('Position', [100, 100, 1000, 600], 'Name', 'Interactive Stochastic Fractional Order Analysis');
% Initial parameters
mu = 0.2;
sigma = 0.2;
s = 100;
dt = 0.01;
N = 1000;
T = N * dt;
% Create sliders and labels for adjusting parameters
uicontrol('Style', 'text', 'Position', [20, 550, 100, 20], 'String', '\mu');
uicontrol('Style', 'slider', 'Min', 0, 'Max', 1, 'Value', mu, 'Position', [120, 550, 300, 20], 'Callback', @(src, event) updatePlot());
uicontrol('Style', 'text', 'Position', [20, 500, 100, 20], 'String', '\sigma');
uicontrol('Style', 'slider', 'Min', 0, 'Max', 1, 'Value', sigma, 'Position', [120, 500, 300, 20], 'Callback', @(src, event) updatePlot());
uicontrol('Style', 'text', 'Position', [20, 450, 100, 20], 'String', 's');
uicontrol('Style', 'slider', 'Min', 100, 'Max', 500, 'Value', s, 'Position', [120, 450, 300, 20], 'Callback', @(src, event) updatePlot());
uicontrol('Style', 'text', 'Position', [20, 400, 100, 20], 'String', '\Delta t');
uicontrol('Style', 'slider', 'Min', 0.001, 'Max', 0.1, 'Value', dt, 'Position', [120, 400, 300, 20], 'Callback', @(src, event) updatePlot());
uicontrol('Style', 'text', 'Position', [20, 350, 100, 20], 'String', 'N_T');
uicontrol('Style', 'slider', 'Min', 100, 'Max', 2000, 'Value', N, 'Position', [120, 350, 300, 20], 'Callback', @(src, event) updatePlot());
% Create axes for plotting
ax = axes('Parent', fig, 'Position', [0.1, 0.1, 0.8, 0.4]);
% Initial plot
set(findobj('String', '\sigma'), 'Value', sigma); % Update the slider value
set(findobj('String', '\mu'), 'Value', mu); % Update the slider value
set(findobj('String', '\Delta t'), 'Value', dt); % Update the slider value
set(findobj('String', 's'), 'Value', s); % Update the slider value
set(findobj('String', 'N_T'), 'Value', N); % Update the slider value
updatePlot(); % Manually call the updatePlot function
% Update plot function
function updatePlot()
% Get current slider values
mu = get(findobj('String', '\mu'), 'Value');
sigma = get(findobj('String', '\sigma'), 'Value');
s = get(findobj('String', 's'), 'Value');
dt = get(findobj('String', '\Delta t'), 'Value');
N = get(findobj('String', 'N_T'), 'Value');
% Run stochastic simulation with updated parameters
alpha = simulateStochasticProcess(mu, sigma, dt, N);
% Define time vector based on the simulation duration
T = N * dt;
t = dt:dt:T;
% Update plot based on simulation results
ax = findobj(gcf, 'Type', 'axes');
plot(ax, t, alpha, 'LineWidth', 1);
xlabel('Time t');
ylabel('Stochastic Order \alpha');
title(['\mu = ', num2str(mu), ', \sigma = ', num2str(sigma), ', s = ', num2str(s), ', \Delta t = ', num2str(dt), ', N_T = ', num2str(N)]);
grid on;
% Update slider values to reflect the changes
set(findobj('String', '\mu'), 'Value', mu);
set(findobj('String', '\sigma'), 'Value', sigma);
set(findobj('String', 's'), 'Value', s);
set(findobj('String', '\Delta t'), 'Value', dt);
set(findobj('String', 'N_T'), 'Value', N);
end
% Function to simulate stochastic process
function alpha = simulateStochasticProcess(mu, sigma, dt, N)
sd = sqrt(dt);
dB = sd * randn(1, N);
B = cumsum(dB);
alpha = mu + sigma * B;
end
I try to display Slider Values when i change these values and visualize the effect on the stochastic process, also each slider shows a corresponding LaTeX text.
how can I do? thanks.

采纳的回答

Voss
Voss 2024-1-7
The labels (i.e., the 'text'-stlye uicontrols) have the strings you are passing to findobj, not the sliders, so findobj returns those text uicontrols, not the slider uicontrols.
In fact, it is not usually necessary to use findobj. Instead, store the uicontrol handles in variables, and refer to those variables later when you want to get properties of the uicontrols. To access those variables in uicontrol callbacks, there are various approaches you can take; I like to have the callbacks nested in a main function, like in the code that follows.
I've included updating the labels' Strings to reflect the current slider Values in updatePlot (which I guess is what you were going for when you were updating the slider Values). LaTeX in a uicontrol text String will not be rendered, but you can use uilabels instead of uicontrols to do it.
GUI() % run the main function
function GUI() % your script is now a function with the other functions nested in it
% Initialize figure
fig = figure('Position', [100, 100, 1000, 600], 'Name', 'Interactive Stochastic Fractional Order Analysis');
% Initial parameters
mu = 0.2;
sigma = 0.2;
s = 100;
dt = 0.01;
N = 1000;
T = N * dt;
% Create sliders and labels for adjusting parameters
h_labels = [ ...
uicontrol('Style', 'text', 'Position', [20, 550, 100, 20]) ...
uicontrol('Style', 'text', 'Position', [20, 500, 100, 20]) ...
uicontrol('Style', 'text', 'Position', [20, 450, 100, 20]) ...
uicontrol('Style', 'text', 'Position', [20, 400, 100, 20]) ...
uicontrol('Style', 'text', 'Position', [20, 350, 100, 20]) ...
];
h_sliders = [ ...
uicontrol('Style', 'slider', 'Min', 0, 'Max', 1, 'Value', mu, 'Position', [120, 550, 300, 20], 'Callback', @(src, event) updatePlot()) ...
uicontrol('Style', 'slider', 'Min', 0, 'Max', 1, 'Value', sigma, 'Position', [120, 500, 300, 20], 'Callback', @(src, event) updatePlot()) ...
uicontrol('Style', 'slider', 'Min', 100, 'Max', 500, 'Value', s, 'Position', [120, 450, 300, 20], 'Callback', @(src, event) updatePlot()) ...
uicontrol('Style', 'slider', 'Min', 0.001, 'Max', 0.1, 'Value', dt, 'Position', [120, 400, 300, 20], 'Callback', @(src, event) updatePlot()) ...
uicontrol('Style', 'slider', 'Min', 100, 'Max', 2000, 'Value', N, 'Position', [120, 350, 300, 20], 'Callback', @(src, event) updatePlot()) ...
];
% Create axes for plotting
ax = axes('Parent', fig, 'Position', [0.1, 0.1, 0.8, 0.4]);
% Initial plot
updatePlot(); % Manually call the updatePlot function
% Update plot function
function updatePlot()
% Get current slider values
mu = get(h_sliders(1),'Value');
sigma = get(h_sliders(2),'Value');
s = get(h_sliders(3),'Value');
dt = get(h_sliders(4),'Value');
N = round(get(h_sliders(5),'Value'));
% Run stochastic simulation with updated parameters
alpha = simulateStochasticProcess(mu, sigma, dt, N);
% Define time vector based on the simulation duration
T = N * dt;
t = dt:dt:T;
% Update plot based on simulation results
plot(ax, t, alpha, 'LineWidth', 1);
xlabel('Time t');
ylabel('Stochastic Order \alpha');
title(['\mu = ', num2str(mu), ', \sigma = ', num2str(sigma), ', s = ', num2str(s), ', \Delta t = ', num2str(dt), ', N_T = ', num2str(N)]);
grid on;
% update the labels:
set(h_labels(1),'String',sprintf('mu = %g',mu));
set(h_labels(2),'String',sprintf('sigma = %g',sigma));
set(h_labels(3),'String',sprintf('s = %g',s));
set(h_labels(4),'String',sprintf('dt = %g',dt));
set(h_labels(5),'String',sprintf('NT = %g',N));
end
% Function to simulate stochastic process
function alpha = simulateStochasticProcess(mu, sigma, dt, N)
sd = sqrt(dt);
dB = sd * randn(1, N);
B = cumsum(dB);
alpha = mu + sigma * B;
end
end
  3 个评论
Yamina chbak
Yamina chbak 2024-1-8
Thanks you @Voss for your help with the code. I really appreciate it!
Voss
Voss 2024-1-10
You're welcome!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by