Remove ignored complex values from a plot
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Arne
am 29 Mär. 2025
Bearbeitet: David Goodmanson
am 1 Apr. 2025
I am trying to plot the generated power of a helium-filled balloon in function of height for different values of a certain parameter. The parameter is the mass per unit length of the used cables (3 kinds). For the heaviest cable the calculated power becomes complex at a certain hight. MATLAB ignores these values and plots them as 0, this however isn't aesthetic. Is there a way i can remove this from the plot?
1 Kommentar
Walter Roberson
am 29 Mär. 2025
For the heaviest cable the calculated power becomes complex at a certain hight. MATLAB ignores these values and plots them as 0
That is not correct.
When you plot() something that is complex-valued, using plot(y), then MATLAB plots real(y) versus imag(y)
When you plot() something that is complex-valued, using plot(x,y) then MATLAB plots real(x) versus real(y) and gives a warning.
If you are seeing 0 plotted there, then it is because the real portion of the data is zero.
(If the program happens to take the square root of a negative number, then the complex value produced would happen to have zero as the real portion, so zero as the real portion is the natural consequence of taking square root of negative numbers.)
Akzeptierte Antwort
Image Analyst
am 29 Mär. 2025
Anything set to nan will not plot so you can set the imaginary locations to nan:
imagLocations = (imag(y) ~= 0)
% Set those locations to nan
x(imagLocations) = nan
y(imagLocations) = nan
% Plot non-nan, non-complex elements.
plot(x, y, 'b.-', 'LineWidth', 3, 'MarkerSize', 25);
0 Kommentare
Weitere Antworten (1)
David Goodmanson
am 30 Mär. 2025
Bearbeitet: David Goodmanson
am 1 Apr. 2025
Hi Arne,
Either you are getting complex numbers because of an error in the algebra that you coded up (I assume you have checked for this), or you have entered a region that is forbidden by the physics and want to exclude it. If you are certain that the complex numbers are due to being in an unphysical region, then you can use something like
ind = (imag(data)==0);
newdata = data(ind)
newheight = height(ind)
to give you a restricted data set and then plot it. Note: Suppose you have some other quantity to plot as a function of all the heights, for example pressure. If height and pressure both have length, say, 100, and newheight and newdata both have length 60, then
plot(height,pressure,newheight,newdata)
gives you two curves on the plot, one of them shorter.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!