Non-existing values in the Plot

2 次查看(过去 30 天)
Greetings. I'm having trouble with some code. This function should display a curve based on X and Y values loaded from a .txt file. For some reason, when it displays the curve it also draws a line in the Y axis. At first I thought I had a problem with the .txt, but i tried plotting it in excel and the curve appears as it should. Also, this does not happen with all the .txt files I'm working with.
Code
function graph
clc;
set(0,'DefaultFigureColor','w',...
'DefaultAxesColor','w',...
'DefaultAxesXColor','k',...
'DefaultAxesYColor','k',...
'DefaultAxesZColor','k',...
'DefaultTextColor','k',...
'DefaultLineColor','k');
x = load('try.txt');
r1 = load('try.txt');
r1 = r1(:,2);
f1=figure(1);
clf;
get(f1);
set(f1,'Units','centimeters','Position',[3 3 24 14]);
axes1 = axes('Parent',f1,'FontWeight','bold','FontSize',14,'Ygrid', 'on','grid', ':','Position',[0.15 0.2 0.8 0.7]);
set(gcf,'Color',[1,1,1]);
box on;
hold on;
axis([0, 16, 0, 6e+06]);
plot(x, r1, '-g','LineWidth',2,'Marker','none', 'Markersize', 5);
hold on
end
This is what my code generates:
This is what excel draws (and what it should be):
Any ideas?

采纳的回答

the cyclist
the cyclist 2014-9-10
Your x variable is both columns of the input file, and r1 is only the second column.
I think you are unintentionally plotting both columns of the input against the second, when you intended to plot the first against the second. The following give what you want:
plot(x(:,1), r1, '-g','LineWidth',2,'Marker','none', 'Markersize', 5);
The following is even better, dispensing with your loading the file twice:
clc;
set(0,'DefaultFigureColor','w',...
'DefaultAxesColor','w',...
'DefaultAxesXColor','k',...
'DefaultAxesYColor','k',...
'DefaultAxesZColor','k',...
'DefaultTextColor','k',...
'DefaultLineColor','k');
x = load('try.txt');
f1=figure(1);
clf;
get(f1);
set(f1,'Units','centimeters','Position',[3 3 24 14]);
axes1 = axes('Parent',f1,'FontWeight','bold','FontSize',14,'Ygrid', 'on','grid', ':','Position',[0.15 0.2 0.8 0.7]);
set(gcf,'Color',[1,1,1]);
box on;
hold on;
axis([0, 16, 0, 6e+06]);
plot(x(:,1), x(:,2), '-g','LineWidth',2,'Marker','none', 'Markersize', 5);
hold on

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by