Create a legend that writes all variable names that I plot

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.

 Accepted Answer

Here's a demo you can follow. The steps are
  1. Wrap inputname() into an anonymous function in order to get variable names.
  2. 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.
  3. After all plotting is done, simply call legend().
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 Comments

Thank you for the help! The legend works appropriately. The only issue I am having now is it shifted all of my planes to 0. I had 3 orthogonal planes centered at a specified point and when I placed the legend code in, it changed the plane orientations. It shows 1 plane at 0 in all 3 directions, and the additional orthogonal planes are shown as lines at the 0 value. Visual representation belowaxes.PNG
How could I align the planes back to the center point and display the entire plane not just one slice of it?
In my demo, only the displayName and it's paird valued getVarname(Vector1) should be added to your plot function calls.
plot(. . ., 'DisplayName', getVarname(Vector1));
That should not change anything other than assigning a DisplayName to your data. Did you change something other than that?
Yes, that's all I did.
I didn't change anything other than adding the getVarname function to the top of my script and for each function call I added 'DisplayName',getVarname(name)
What I orginally had:
plane1=[p0 v2 v3];
plane2=[p0 v1 v2];
plane3=[p0 v1 v3];
drawPlane3d(plane1)
drawPlane3d(plane2)
drawPlane3d(plane3)
What I added:
getVarname = @(x) inputname(1)
plane1=[p0 v2 v3];
plane2=[p0 v1 v2];
plane3=[p0 v1 v3];
drawPlane3d(plane1, 'DisplayName', getVarname(plane1))
drawPlane3d(plane2, 'DisplayName', getVarname(plane2))
drawPlane3d(plane3, 'DisplayName', getVarname(plane3))
legend()
The plane inputs stayed the same.
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))
Thank you! That seemed to fix it.

Sign in to comment.

More Answers (0)

Asked:

on 18 Dec 2019

Commented:

on 23 Dec 2019

Community Treasure Hunt

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

Start Hunting!