Azzera filtri
Azzera filtri

Want to remove a line in my plot

27 visualizzazioni (ultimi 30 giorni)
Grace
Grace il 15 Dic 2021
Commentato: Star Strider il 18 Dic 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

Risposta accettata

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

Accedi per commentare.

Più risposte (1)

Voss
Voss il 15 Dic 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 Commento
Grace
Grace il 16 Dic 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

Accedi per commentare.

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by