GUI plot

1 次查看(过去 30 天)
Ean Soo
Ean Soo 2011-3-19
回答: Sanchari 2024-7-25
Hie guys, i imported an excel file into my gui and i plotted it out together with the vertical lines that i need. However, with the same code, it does not work for the other axes in the same GUI.Below are my codes,
--------first axes code----------
plot(handles.axes1,c,d);
hold on;
XData=get(get(handles.axes1,'children'),'XData');
YData=get(get(handles.axes1,'children'),'YData');
y_BPFO = interp1(XData,YData,BPFO);
t=get(handles.axes1,'ylim');
plot([BPFO BPFO],t,'-y');
-----------second axes(in the same GUI)--------
plot(handles.axes2,c,d);
hold on;
XData1=get(get(handles.axes2,'children'),'XData');
YData1=get(get(handles.axes2,'children'),'YData');
y_BPFO1 = interp1(XData1,YData1,BPFO1);
q=get(handles.axes2,'ylim');
plot([BPFO1 BPFO1],q,'-y');
--------------------------------------------
I manage to get the first plot with all the data and vertical lines. However, i cant get the plot for the second axes. Can someone help me out?
Regards,
Ean
  1 个评论
Jan
Jan 2011-3-19
What does "I cant get" mean exactly? Do you get an error message or do the results differ from your expectations? Please explain such details.

请先登录,再进行评论。

回答(1 个)

Sanchari
Sanchari 2024-7-25
Hello Ean,
It seems like the question is about how to plot data on two different axes within the same GUI and add vertical lines at specific points. From the description given, the first plot works correctly but the second one does not.
There are a few potential issues that could be causing the problem:
  1. Data Range and Interpolation: Ensure that "BPFO1" falls within the range of "XData1". If "BPFO1" is outside the range, "interp1" will return "NaN".
  2. Axes Handle: Ensure that "handles.axes2" is correctly referencing the second axes in your GUI.
  3. Hold State: Ensure that "hold on" is correctly applied to "handles.axes2".
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The revised code is provided below assuming a sample data and with some additional checks and debugging information to help identify the issue:
% Sample data generation
c = linspace(0, 2*pi, 100); % XData
d = sin(c); % YData
% Sample BPFO and BPFO1 values
BPFO = pi/2; % Example value within the range of XData
BPFO1 = 3*pi/2; % Another example value within the range of XData
% Create a figure with two axes
figure;
handles.axes1 = subplot(2, 1, 1); % First axes
handles.axes2 = subplot(2, 1, 2); % Second axes
% First axes code
plot(handles.axes1, c, d);
hold(handles.axes1, 'on');
XData = get(get(handles.axes1, 'children'), 'XData');
YData = get(get(handles.axes1, 'children'), 'YData');
y_BPFO = interp1(XData, YData, BPFO);
t = get(handles.axes1, 'ylim');
plot(handles.axes1, [BPFO BPFO], t, '-y');
% Check if the interpolation was successful
if isnan(y_BPFO)
disp('Warning: y_BPFO is NaN. Check if BPFO is within the range of XData.');
end
% Second axes code
plot(handles.axes2, c, d);
hold(handles.axes2, 'on');
XData1 = get(get(handles.axes2, 'children'), 'XData');
YData1 = get(get(handles.axes2, 'children'), 'YData');
y_BPFO1 = interp1(XData1, YData1, BPFO1);
q = get(handles.axes2, 'ylim');
plot(handles.axes2, [BPFO1 BPFO1], q, '-y');
% Check if the interpolation was successful
if isnan(y_BPFO1)
disp('Warning: y_BPFO1 is NaN. Check if BPFO1 is within the range of XData1.');
end
% Ensure the axes are held correctly
hold(handles.axes1, 'off');
hold(handles.axes2, 'off');
% Debugging information
disp('First axes data:');
disp(['XData: ', num2str(XData)]);
disp(['YData: ', num2str(YData)]);
disp(['BPFO: ', num2str(BPFO)]);
disp(['y_BPFO: ', num2str(y_BPFO)]);
disp('Second axes data:');
disp(['XData1: ', num2str(XData1)]);
disp(['YData1: ', num2str(YData1)]);
disp(['BPFO1: ', num2str(BPFO1)]);
disp(['y_BPFO1: ', num2str(y_BPFO1)]);
Output the above code:
First axes data:
XData: 0 0.063467 0.12693 0.1904 0.25387 0.31733 0.3808 0.44427 0.50773 0.5712 0.63467 0.69813 0.7616 0.82506 0.88853 0.952 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.904 1.9675 2.0309 2.0944 2.1579 2.2213 2.2848 2.3483 2.4117 2.4752 2.5387 2.6021 2.6656 2.7291 2.7925 2.856 2.9195 2.9829 3.0464 3.1099 3.1733 3.2368 3.3003 3.3637 3.4272 3.4907 3.5541 3.6176 3.6811 3.7445 3.808 3.8715 3.9349 3.9984 4.0619 4.1253 4.1888 4.2523 4.3157 4.3792 4.4427 4.5061 4.5696 4.6331 4.6965 4.76 4.8235 4.8869 4.9504 5.0139 5.0773 5.1408 5.2043 5.2677 5.3312 5.3947 5.4581 5.5216 5.5851 5.6485 5.712 5.7755 5.8389 5.9024 5.9659 6.0293 6.0928 6.1563 6.2197 6.2832
YData: 0 0.063424 0.12659 0.18925 0.25115 0.31203 0.37166 0.42979 0.4862 0.54064 0.59291 0.64279 0.69008 0.73459 0.77615 0.81458 0.84973 0.88145 0.90963 0.93415 0.9549 0.97181 0.98481 0.99384 0.99887 0.99987 0.99685 0.98982 0.9788 0.96384 0.945 0.92235 0.89599 0.86603 0.83257 0.79576 0.75575 0.71269 0.66677 0.61816 0.56706 0.51368 0.45823 0.40093 0.34202 0.28173 0.22031 0.158 0.095056 0.031728 -0.031728 -0.095056 -0.158 -0.22031 -0.28173 -0.34202 -0.40093 -0.45823 -0.51368 -0.56706 -0.61816 -0.66677 -0.71269 -0.75575 -0.79576 -0.83257 -0.86603 -0.89599 -0.92235 -0.945 -0.96384 -0.9788 -0.98982 -0.99685 -0.99987 -0.99887 -0.99384 -0.98481 -0.97181 -0.9549 -0.93415 -0.90963 -0.88145 -0.84973 -0.81458 -0.77615 -0.73459 -0.69008 -0.64279 -0.59291 -0.54064 -0.4862 -0.42979 -0.37166 -0.31203 -0.25115 -0.18925 -0.12659 -0.063424 -2.4493e-16
BPFO: 1.5708
y_BPFO: 0.99962
Second axes data:
XData1: 0 0.063467 0.12693 0.1904 0.25387 0.31733 0.3808 0.44427 0.50773 0.5712 0.63467 0.69813 0.7616 0.82506 0.88853 0.952 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.904 1.9675 2.0309 2.0944 2.1579 2.2213 2.2848 2.3483 2.4117 2.4752 2.5387 2.6021 2.6656 2.7291 2.7925 2.856 2.9195 2.9829 3.0464 3.1099 3.1733 3.2368 3.3003 3.3637 3.4272 3.4907 3.5541 3.6176 3.6811 3.7445 3.808 3.8715 3.9349 3.9984 4.0619 4.1253 4.1888 4.2523 4.3157 4.3792 4.4427 4.5061 4.5696 4.6331 4.6965 4.76 4.8235 4.8869 4.9504 5.0139 5.0773 5.1408 5.2043 5.2677 5.3312 5.3947 5.4581 5.5216 5.5851 5.6485 5.712 5.7755 5.8389 5.9024 5.9659 6.0293 6.0928 6.1563 6.2197 6.2832
YData1: 0 0.063424 0.12659 0.18925 0.25115 0.31203 0.37166 0.42979 0.4862 0.54064 0.59291 0.64279 0.69008 0.73459 0.77615 0.81458 0.84973 0.88145 0.90963 0.93415 0.9549 0.97181 0.98481 0.99384 0.99887 0.99987 0.99685 0.98982 0.9788 0.96384 0.945 0.92235 0.89599 0.86603 0.83257 0.79576 0.75575 0.71269 0.66677 0.61816 0.56706 0.51368 0.45823 0.40093 0.34202 0.28173 0.22031 0.158 0.095056 0.031728 -0.031728 -0.095056 -0.158 -0.22031 -0.28173 -0.34202 -0.40093 -0.45823 -0.51368 -0.56706 -0.61816 -0.66677 -0.71269 -0.75575 -0.79576 -0.83257 -0.86603 -0.89599 -0.92235 -0.945 -0.96384 -0.9788 -0.98982 -0.99685 -0.99987 -0.99887 -0.99384 -0.98481 -0.97181 -0.9549 -0.93415 -0.90963 -0.88145 -0.84973 -0.81458 -0.77615 -0.73459 -0.69008 -0.64279 -0.59291 -0.54064 -0.4862 -0.42979 -0.37166 -0.31203 -0.25115 -0.18925 -0.12659 -0.063424 -2.4493e-16
BPFO1: 4.7124
y_BPFO1: -0.99962
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Explanation:
  1. Hold State: Use "hold(handles.axes1, 'on')" and "hold(handles.axes2, 'on')" to ensure the hold state is applied to the correct axes.
  2. Interpolation Check: Add a check to see if "interp1" returns "NaN", which would indicate that "BPFO" or "BPFO1" is outside the range of "XData" or "XData1". In the above code, they are set to "pi/2" and "3*pi/2", respectively, which are within the range of "c".
  3. Axes Handle: Create the figure with two subplots for "axes1" and "axes2". Ensure that the correct axes handles (handles.axes1 and handles.axes2) are being referenced.
  4. Debugging Information: Print the data to the script like given or to the MATLAB command window to verify the values.
By adding these checks and debugging information, you should be able to identify why the second plot is not displaying correctly and make the necessary adjustments.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by