Real time plot trend tweaking option
조회 수: 4 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Pratyush Swain
2023년 11월 27일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!