Want to remove a line in my plot

43 views (last 30 days)
Grace
Grace on 15 Dec 2021
Commented: Star Strider on 18 Dec 2021
Hi, pls I need to remove a straight line in my plot. I attached the two files ('A.txt' and 'B.txt') that I am analyzing. Could someone help out on this;
This is my code;
A=load('A.txt');B=load('B.txt');
X=(B(:,2)-A(:,2))/A(:,2);
plot(A(:,1),X, 'k');xlim([780 820])
Thank you

Accepted Answer

Star Strider
Star Strider on 15 Dec 2021
A = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/835480/A.txt');
A = A(:,[2 3]);
B = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/835485/B.txt');
X=(B(:,2)-A(:,2))./A(:,2);
figure
plot(A(:,1),X, 'k');
xlim([780 820])
It is not the same plot, and there is no horizontal line.
Since the data are probably in columns in the original plot as they are here, one option is:
AX = sortrows([A X],1)
A1 = AX(:,1)
X = AX(:,2)
figure
plot(A1, X, 'k');
xlim([780 820])
and see if that works, since it appears that the data are ‘wrapping’, and the sortrows call should eliminate that.
.
  4 Comments
Grace
Grace on 18 Dec 2021
Hi, Star.... Thank you very much
Star Strider
Star Strider on 18 Dec 2021
As always, my pleasure!
.

Sign in to comment.

More Answers (1)

Voss
Voss on 15 Dec 2021
You should change the calculation of X to be:
X=(B(:,2)-A(:,2))./A(:,2);
This does element-wise division rather than matrix division.
If you check the size and value of X with the calculation the way you had it, you would see that X was a 2048-by-2048 matrix, so when you plot it against A(:,1), you are actually plotting 2048 lines, all but one of which are all zeros (the flat line).
With element-wise division, X is a column vector and one line is plotted, which is, I believe, what is intended.
  1 Comment
Grace
Grace on 16 Dec 2021
Thank you Benjamin. I think, the element wise operation is best for me because it preserves the shape the shape of my expected result. But by performint the element-wise operation, the true shape of the data is lost.
So, I am still thinking on how to correct this error

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!