How to solve "Line cannot be a child of Figure." error?

125 Ansichten (letzte 30 Tage)
Pedro Augusto de Castro e Castro
Kommentiert: Steven Lord am 20 Sep. 2023
So, I have a problem trying to plot 2 points in a 3D graph. I'm using the following code:
fig_UPS = figure;
hold on
plot3(fig_UPS, handles.VolDensPot_UPS, handles.PesoDensPot_UPS, handles.Rend_UPS, 'Color', 'r', 'Marker', '*','DisplayName', handles.Nome_UPS, 'LineStyle', 'none');
hold on
plot3(fig_UPS, handles.VolDensPot_Wendell, handles.PesoDensPot_Wendell, handles.Rend_Wendell, 'Color', 'b', 'Marker', 'o','DisplayName', handles.Nome_Wendell, 'LineStyle', 'none');
legend(fig_UPS);
xlabel(fig_UPS, 'Densidade de Potência Volumétrica [kW/dm³]');
ylabel(fig_UPS, 'Densidade de Potência Mássica [kW/kg]');
zlabel(fig_UPS, 'Rendimento')
But I've being getting the "Line cannot be a child of Figure." error.
How can I solve this?
Thanks in advance

Akzeptierte Antwort

Rik
Rik am 18 Jun. 2020
You need to create an axes object first so you can use that as the parent object instead.
fig_UPS = figure;
ax=axes('Parent',fig_UPS);
hold(ax,'on')
plot3(ax, handles.VolDensPot_UPS, handles.PesoDensPot_UPS, handles.Rend_UPS, 'Color', 'r', 'Marker', '*','DisplayName', handles.Nome_UPS, 'LineStyle', 'none');
plot3(ax, handles.VolDensPot_Wendell, handles.PesoDensPot_Wendell, handles.Rend_Wendell, 'Color', 'b', 'Marker', 'o','DisplayName', handles.Nome_Wendell, 'LineStyle', 'none');
legend(ax);
xlabel(ax, 'Densidade de Potência Volumétrica [kW/dm³]');
ylabel(ax, 'Densidade de Potência Mássica [kW/kg]');
zlabel(ax, 'Rendimento')
  3 Kommentare
Jorge
Jorge am 20 Sep. 2023
but why? I don't know how I would have gotten this from the help page on "plot".
So, what is the handle fig_UPS in the code above referring to if is not the axes of figure?
Steven Lord
Steven Lord am 20 Sep. 2023
If you look at the Syntax section of the documentation for the plot function, what can the first input argument be? It can be X, X1, Y, tbl, or ax. [I'm going to ignore the Name, Value syntax because I don't want to go through each of the name-value arguments. Trust me that a figure can't be a Name.] What can each of those be? In the Input Arguments section, we see that:
  • X - x-coordinates. The entry for the X input argument lists the data types supported for that input.
  • X1 - same thing as X, just allowing you to specify multiple sets of x- and y- coordinates in one call.
  • Y - y-coordinates. The entry for the Y input argument lists the data types supported for that input.
  • tbl - Source table, allowed to be either a table or timetable.
  • ax - Target axes, allowed to be an Axes object, a PolarAxes object, or a GeographicAxes object.
Does the output of the figure function match the requirements of being any of those input arguments?
f = figure;
class(f)
ans = 'matlab.ui.Figure'
No, f is not one of the types supported for the X or Y inputs. It's not a table or timetable so it's not the tbl input argument. It's not an Axes, PolarAxes, or GeographicAxes object so it's not the ax input argument. Therefore there's no syntax listed for plot that supports passing f in as the first input. [I am ignoring the Name, Value syntax. f is not a valid Name.]
handle.fig_UPS is a matlab.ui.Figure handle (commonly abbreviated either Figure or figure.) Lines (like the one returned by plot can be contain in Axes, PolarAxes, Group, or Transform objects. [The list in the description of the Parent property of lines omits GeographicAxes but I believe it should include that type. I'll report that to the documentation staff.] Figures can contain Axes, PolarAxes, Group, or Transform objects but are not themselves Axes, PolarAxes, Group, or Transform objects.
Generally speaking, if you look at the picture on this documentation page, objects of types nearer the top of the diagram can contain (be the Parent of) objects below them in the diagram that are connected to them by lines.
If you have young kids you wouldn't want them to draw a line directly on the wall (drawing in a figure directly) but having them draw on a piece of paper attached to the wall (an axes in the figure) is more likely to be acceptable behavior.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

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