Filter löschen
Filter löschen

Want to remove a line in my plot

27 Ansichten (letzte 30 Tage)
Grace
Grace am 15 Dez. 2021
Kommentiert: Star Strider am 18 Dez. 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

Akzeptierte Antwort

Star Strider
Star Strider am 15 Dez. 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 Kommentare
Grace
Grace am 18 Dez. 2021
Hi, Star.... Thank you very much
Star Strider
Star Strider am 18 Dez. 2021
As always, my pleasure!
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Voss
Voss am 15 Dez. 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 Kommentar
Grace
Grace am 16 Dez. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by