How could I use "batteryChart" to plot Simscape battery objects on UIAxes in AppDesigner?
4 Comments
Accepted Answer
9 Comments
- Same Object Type: Both figure and uifigure commands create objects of the same type, matlab.ui.Figure. This is why they both have the Type property set to 'figure'. The distinction between them lies primarily in their default property values, such as HandleVisibility and AutoResizeChildren, rather than in their fundamental object type.
- Current Differences: While there are some differences in functionalities between figure and uifigure due to their historical development paths, these are temporary. The goal is to unify these functionalities in a future release.
- Using findobj: The results from using findobj are influenced by the HandleVisibility property. Typically, uifigure objects are created with different HandleVisibility (is "off" for uifigures, which is why you did not find these) settings compared to figure objects. This is intentional, as uifigure is often used for building apps where you might not want all components to appear in findobj results.
- In an upcoming release, the differences between figure and uifigure will mainly be about the default property values upon creation. The long-term aim is to make them functionally equivalent, making it unnecessary to differentiate between them.
More Answers (1)
Hi @Hartmuth,
You mentioned, “Any help or workaround would be highly appreciated!”
After reviewing your comments, it sounds like that the challenge you are facing arises from the fact that the batteryChart function is designed to work with Figure objects, while AppDesigner primarily utilizes UIAxes for plotting. However, there are workarounds that can help you achieve your goal. One effective method is to create a hidden Figure object in the background, plot the battery chart there, and then transfer the visual output to the UIAxes. Below is a detailed step-by-step guide along with a complete code example.
Create a Hidden Figure: This figure will serve as the parent for the batteryChart.
Plot the Battery Chart: Use the batteryChart function to plot the battery data on the hidden figure.
Copy the Chart to UIAxes: Extract the chart data and render it on the UIAxes.
Here is a complete example that demonstrates how to implement this workaround in AppDesigner:
classdef BatteryApp < matlab.apps.AppBase
% Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure UIAxes matlab.ui.control.UIAxes PlotButton matlab.ui.control.Button end
properties (Access = private) Cell % Battery cell object end
methods (Access = private)
% Button pushed function: PlotButton function PlotButtonPushed(app, event) % Create a hidden figure hiddenFig = figure('Visible', 'off');
% Create a battery cell object (example parameters) app.Cell = simscape.battery.Cell('NominalVoltage', 3.7, ... 'Capacity', 2.5, ... 'InternalResistance', 0.05);
% Plot the battery chart on the hidden figure batteryChart(hiddenFig, app.Cell);
% Get the current axes from the hidden figure ax = hiddenFig.CurrentAxes;
% Copy the chart data to the UIAxes copyobj(allchild(ax), app.UIAxes);
% Close the hidden figure close(hiddenFig); end end
% Construct app methods (Access = public)
% Create UI components and initialize the app function app = BatteryApp % Create and configure components createComponents(app) end
% Create UI components function createComponents(app) % Create UIFigure app.UIFigure = uifigure('Visible', 'off'); app.UIFigure.Position = [100 100 400 300]; app.UIFigure.Name = 'Battery Chart App';
% Create UIAxes app.UIAxes = uiaxes(app.UIFigure); app.UIAxes.Position = [50 50 300 200];
% Create PlotButton app.PlotButton = uibutton(app.UIFigure, 'push'); app.PlotButton.Position = [150 10 100 30]; app.PlotButton.Text = 'Plot Battery'; app.PlotButton.ButtonPushedFcn = @(src, event) PlotButtonPushed(app, event);
% Show the figure after all components are created app.UIFigure.Visible = 'on'; end end end
So, in the above code, the main application class inherits from matlab.apps.AppBase. The app contains properties for the UI components and the battery cell object. PlotButtonPushed Method is triggered when the button is pressed. It creates a hidden figure, plots the battery chart, and then copies the chart to the UIAxes and createComponents Method initializes the UI components, including the figure, axes, and button.
By following the above steps and utilizing the provided code, you can successfully plot battery objects using batteryChart within AppDesigner. This workaround will allow you to leverage the capabilities of batteryChart while adhering to the UI design principles of AppDesigner.
Hope this helps.
If you have any further questions or need additional assistance, feel free to ask!
5 Comments
Hi @Hartmuth,
If that is the case then it should not have provided link to mathworks. I am trying to help you out here and this is how you are appreciating my contributions.
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!