Real time plot trend tweaking option
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi
I have some data, and the trend line is as shown below. Is there any way to adjust the trend line using my PC mouse and see the effect on the coefficients? This is more of GUI question
0 Commenti
Risposte (1)
Pratyush Swain
il 27 Nov 2023
Hi Dharmesh,
I understand you want to make changes to your quadratic line plot in real time and make observations. There is no direct method in which you can modify the line by your mouse through dragging and clicking but one workaround for this task can be to place ui sliders for coefficients in the plot and observe the changes in the quadratric fit line by changing the slider(coefficients) values.
Please refer to below implementation for the same:
% Generate sample data
x = 1:10;
% Equation goes like Ax^2 + Bx + C
y = -0.4*x.^2 - 0.122*x + 246 + randn(size(x));
% Create a figure
fig = figure;
% Plot the scatter data
scatter(x, y, 'b', 'filled');
hold on;
% Initialize coefficients
A = -0.4;
B = -0.122;
C = 246;
% Plot the initial quadratic best fit line
x_fit = linspace(min(x), max(x), 100);
% Used the initial coefficients found on your plot
y_fit = A*x_fit.^2 + B*x_fit + C;
fitLine = plot(x_fit, y_fit, 'r', 'LineWidth', 2);
% Store coefficients in figure's appdata
setappdata(fig, 'A', A);
setappdata(fig, 'B', B);
setappdata(fig, 'C', C);
% Store the fit liine in the app data too
setappdata(fig,'fitLine',fitLine);
uicontrol('Style', 'slider', 'Min', -1, 'Max', 1, 'Value', A, 'Position', [400, 350, 120, 20], 'Callback', {@updateFitLine, 'A' , x_fit});
uicontrol('Style', 'slider', 'Min', -1, 'Max', 1, 'Value', B, 'Position', [400, 320, 120, 20], 'Callback', {@updateFitLine, 'B', x_fit});
uicontrol('Style', 'slider', 'Min', 100, 'Max', 500, 'Value', C, 'Position', [400, 380, 120, 20], 'Callback', {@updateFitLine, 'C', x_fit});
% Label the sliders
uicontrol('Style', 'text', 'String', 'A', 'Position', [370, 350, 20, 20]);
uicontrol('Style', 'text', 'String', 'B', 'Position', [370, 320, 20, 20]);
uicontrol('Style', 'text', 'String', 'C', 'Position', [370, 380, 20, 20]);
% Callback function to update the fit line based on the new coefficients
function updateFitLine(src, ~, coefficient,x_fit)
value = src.Value;
fig = ancestor(src, 'figure');
% Update the coefficient and store in figure's appdata
switch coefficient
case 'A'
setappdata(fig, 'A', value);
case 'B'
setappdata(fig, 'B', value);
case 'C'
setappdata(fig, 'C', value);
end
% Retrieve coefficients from figure's appdata
A = getappdata(fig, 'A');
B = getappdata(fig, 'B');
C = getappdata(fig, 'C');
% Update the fit line
fitLine = getappdata(fig,'fitLine');
y_fit = A*x_fit.^2 + B*x_fit + C;
set(fitLine, 'YData', y_fit);
setappdata(fig,'fitLine',fitLine);
end
For more information please refer to:
Hope this helps.
0 Commenti
Vedere anche
Categorie
Scopri di più su Get Started with MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!