Mastering Legends in MATLAB App Designer's UIAxes

MATLAB App Designer is a powerful tool for building custom applications with interactive user interfaces. One crucial aspect of visualizing data effectively within these apps is the use of legends in UIAxes. Legends help users understand what different data series represent, making complex visualizations more accessible and interpretable. This article delves into how to add and customize legends in UIAxes within MATLAB App Designer, offering practical tips and examples to enhance your applications.

Introduction to UIAxes and Legends

MATLAB's App Designer provides a rich environment for creating sophisticated user interfaces. UIAxes is a component within App Designer that enables the creation of interactive plots and charts. Legends, an essential feature of many plots, serve to label different data series, improving clarity and usability.

Adding Legends to UIAxes

To add a legend to a UIAxes component in App Designer, follow these steps:

  1. Create a UIAxes Component:

    • Open MATLAB App Designer and create a new app or open an existing one.
    • Drag a UIAxes component from the Component Library onto your app's canvas.
  2. Plot Data on UIAxes:

    • Use the plot function to add data series to your UIAxes. For instance:
      matlab
      plot(app.UIAxes, x1, y1, '-r', 'DisplayName', 'Series 1'); hold(app.UIAxes, 'on'); plot(app.UIAxes, x2, y2, '--b', 'DisplayName', 'Series 2'); hold(app.UIAxes, 'off');
  3. Add a Legend:

    • To add a legend, use the legend function and specify the UIAxes you want to apply it to:
      matlab
      legend(app.UIAxes, 'show');

Customizing Legends

Legends can be customized to enhance readability and fit the style of your application. Here are some key customization options:

  1. Positioning:

    • You can specify the position of the legend using the 'Location' parameter:
      matlab
      legend(app.UIAxes, 'Location', 'northeast');
  2. Font and Size:

    • Adjust the font size and style to match your application's design:
      matlab
      legend(app.UIAxes, 'FontSize', 12, 'FontWeight', 'bold');
  3. Legend Labels:

    • Customize legend labels directly in the plot function using the 'DisplayName' property. Ensure each plot command has a unique 'DisplayName' that will be used in the legend.
  4. Legend Box:

    • Control the appearance of the legend box, including its visibility and edge color:
      matlab
      lgd = legend(app.UIAxes); lgd.Box = 'off'; lgd.EdgeColor = 'red';

Practical Example

Let's put these concepts into practice with a simple example:

  1. Create Sample Data:

    matlab
    x1 = 1:10; y1 = sin(x1); x2 = 1:10; y2 = cos(x2);
  2. Plot Data with Legends:

    matlab
    % Plotting in UIAxes plot(app.UIAxes, x1, y1, '-r', 'DisplayName', 'Sine Wave'); hold(app.UIAxes, 'on'); plot(app.UIAxes, x2, y2, '--b', 'DisplayName', 'Cosine Wave'); hold(app.UIAxes, 'off'); % Adding a legend legend(app.UIAxes, 'show');
  3. Customize the Legend:

    matlab
    lgd = legend(app.UIAxes); lgd.Location = 'northeast'; lgd.FontSize = 12; lgd.FontWeight = 'bold'; lgd.Box = 'off';

Conclusion

Legends are a fundamental aspect of data visualization in MATLAB App Designer. By effectively adding and customizing legends in UIAxes, you can significantly enhance the clarity and user experience of your applications. Whether you're plotting simple data or complex datasets, understanding how to leverage legends will make your visualizations more informative and user-friendly.

Keep Exploring

Experiment with different legend properties to find the best configuration for your app. MATLAB's documentation and community forums offer additional resources and examples to further refine your skills in using UIAxes and legends.

Popular Comments
    No Comments Yet
Comment

0