Remove ignored complex values from a plot

조회 수: 9 (최근 30일)
Arne
Arne 2025년 3월 29일
편집: David Goodmanson 2025년 4월 1일
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
Walter Roberson
Walter Roberson 2025년 3월 29일
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.)

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2025년 3월 29일
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);

추가 답변 (1개)

David Goodmanson
David Goodmanson 2025년 3월 30일
편집: David Goodmanson 2025년 4월 1일
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.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by