Hi, i want to plot two columns of values of data from excel, the graph includes a zero while the excel sheet doesn't. Here is my code and the graph I got vs what I should get
7 次查看(过去 30 天)
显示 更早的评论
This is my code
clear all;
clc;
% Load data from Excel
data = xlsread('Results_1.xlsx'); % Replace 'Results_1.xlsx' with your file name
% Extract columns from the data
q = data(:, 2); % Assuming q is in the second column
D_q = data(:, 5); % Assuming D_q is in the fifth column
errors = data(:, 6); % Assuming errors in D_q are in the sixth column
% Plot with error bars and colored line
errorbar(q, D_q, errors, 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'b', 'MarkerEdgeColor', 'b');
hold on; % Keep the current graph and add new data to it
plot(q, D_q, 'Color', 'r', 'LineWidth', 2); % Red colored line
% Customize axis labels
xlabel('q', 'FontSize', 14); % Customize the font size
ylabel('D_q', 'FontSize', 14); % Customize the font size
% Set x-axis and y-axis limits to exclude the origin
xlim([-11, 11]); % Adjust the limits according to your data range
ylim([1.5, 4]); % Adjust the limits according to your data range
grid on;
What I got :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1668846/image.jpeg)
What I should get (similar to it):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1668851/image.png)
My Exel sheet
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1668856/image.png)
采纳的回答
Mathieu NOE
2024-4-16
编辑:Mathieu NOE
2024-4-16
hello again
so your arrays contains 0 after row 22 til the end, that's why your plot goes back to the origin point (this was already pointed out by @Fangjun Jiang in his comment above)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1669941/image.png)
you need to remove the invalid data from your plot
the first option is to simply specify which rows you want to plot - here we need row 1 to 21
clear all;
clc;
% Load data from Excel
data = xlsread('Results_1.xlsx'); % Replace 'Results_1.xlsx' with your file name
% Extract columns from the data
rows = (1:21); % define valid rows (non zeros in all columns)
q = data(rows, 2); % Assuming q is in the second column
D_q = data(rows, 5); % Assuming D_q is in the fifth column
errors = data(rows, 6); % Assuming errors in D_q are in the sixth column
% Plot with error bars and colored line
errorbar(q, D_q, errors, 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'b', 'MarkerEdgeColor', 'b');
hold on; % Keep the current graph and add new data to it
plot(q, D_q, 'Color', 'r', 'LineWidth', 2); % Red colored line
% Customize axis labels
xlabel('q', 'FontSize', 14); % Customize the font size
ylabel('D_q', 'FontSize', 14); % Customize the font size
% Set x-axis and y-axis limits to exclude the origin
xlim([-11, 11]); % Adjust the limits according to your data range
ylim([1.5, 4]); % Adjust the limits according to your data range
grid on;
3 个评论
更多回答(1 个)
Fangjun Jiang
2024-4-15
I think the last data point (un-wanted) is at (0,0). Your data shows it. You can look at the value of q and D_q.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!