How to Use a Variable Name in Matlab Legend

Save

Many scientists and engineers use Matlab, a high-level programming language and development environment developed by the Mathworks, to quickly perform numerical computation, data analysis and data visualization.


Matlab is used in a wide variety of applications ranging from financial modeling to signal and image analysis. You may need to visualize your data using Matlab's extensive graphics capabilities. In order to make sure your graphics are understandable, you might need to add variable names to legend entries on your plots.

  • Type "x = 1:(2pi)/100:(2pi)" in the command window to generate a range of x values to plot.

  • Type "sinfunction = sin(x)" to calculate the y values for the first function. Alternatively, use your own function of x in place of "sin(x)."

  • Type "plot1 = plot(x,sinfunction,'DisplayName','sinfunction')" to plot the first function. To use a different name, change the second "sinfunction." For example, type "plot1 = plot(x,sinfunction,'DisplayName','1st function')" to label the legend entry as "1st function."

  • Type "hold on" to plot the second function on the same axes.

  • Type "cosfunction = cos(x)" to calculate the y values for the second function. Alternatively, use your own function of x in place of "cos(x)."

  • Type "plot2 = plot(x,cosfunction,'DisplayName','cosfunction')" to plot the second function. To use a different name, change the second "cosfunction."

  • Type "legend_handle = legend('on')" to show the legend on the figure.

Tips & Warnings

  • You can move the legend by pressing the left mouse button while the cursor is over the legend and dragging it to a new location. Alternatively, you can specify the position by typing "legend(legend_handle,'Location','position') where "position" is replaced with "NorthEast," "NorthWest," "SouthEast" and so on. Type "help legend" to see all allowed values for "position."

References

  • Photo Credit Thinkstock/Comstock/Getty Images
Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!