Non-existing values in the Plot

2 views (last 30 days)
Pedro
Pedro on 10 Sep 2014
Commented: Pedro on 10 Sep 2014
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?

Accepted Answer

the cyclist
the cyclist on 10 Sep 2014
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 Comment
Pedro
Pedro on 10 Sep 2014
Everything's works fine, now! Thank you for your help.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!