Non-existing values in the Plot

조회 수: 2 (최근 30일)
Pedro
Pedro 2014년 9월 10일
댓글: Pedro 2014년 9월 10일
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
  댓글 수: 1
Pedro
Pedro 2014년 9월 10일
Everything's works fine, now! Thank you for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (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