- Wrap inputname() into an anonymous function in order to get variable names.
- Plot the data and include the DisplayName property (just about all plotting functions have this property). For the DisplayName value, use the function in step 1 and input the variable.
- After all plotting is done, simply call legend().
Create a legend that writes all variable names that I plot
83 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lauren
el 18 de Dic. de 2019
Comentada: Lauren
el 23 de Dic. de 2019
Hi, I am trying to create a legend in my code that automatically writes the names of variables that I plot to a single figure and will update accordingly if I add or remove items.
I am plotting various types of data (i.e. planes, vectors, and point clouds) using drawPlane3d(name of plane), drawVector3d(center, direction and length), and drawPoint3d(name of point cloud). Right now I am manually updating the legend in the order that items are plotted.
For example:
drawVector3d(center,Vector1*50)
drawVector3d(center,Vector2*50)
drawPoint3d(nameofpointcloud)
drawPlane3d(Plane1)
legend('Vector1', 'Vector2', 'nameofpointcloud', 'Plane1')
Is there a way I could use a loop or sprintf to identify the variable names being plotted and write the names of the variables automatically to the legend? The plotted variable names are not in a string right now, they are just seperate variables that share a common command format draw(typeof data you want to plot)3d.
0 comentarios
Respuesta aceptada
Adam Danz
el 18 de Dic. de 2019
Editada: Adam Danz
el 19 de Dic. de 2019
Here's a demo you can follow. The steps are
Vector1 = 1:10; % demo data
% STEP 1
getVarname = @(x) inputname(1); % function to get variable name
% STEP 2
plot(Vector1, 'DisplayName', getVarname(Vector1));
% STEP 3
legend()
5 comentarios
Adam Danz
el 23 de Dic. de 2019
What is drawPlane3d? That's not a function provided by Matlab. If it's referring to this function, it looks like the patch object handles are provided in the output. You'll need to assign the DisplayName to those objects after calling that function.
h = drawVector3d(. . .);
h.DisplayName = getVarname(plane1)
If h is an array of handles,
set(h, 'DisplayName', getVarname(plane1))
Más respuestas (0)
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!