How could I use "batteryChart" to plot Simscape battery objects on UIAxes in AppDesigner?
4 Comments
Accepted Answer
More Answers (1)
0 votes
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
endmethods (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
endSo, 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,
Thank you for your detailed feedback regarding the issues with the BatteryChart class and your clarification on cell object creation syntax. Let me address both concerns systematically. Since the BatteryChart class does not have a Children property, you cannot use copyobj to transfer its contents directly to UIAxes. Here are some alternative approaches you can consider: Instead of copying, you can create a new chart in your UIAxes by instantiating a new BatteryChart object directly in the desired axes. For example:
% Assuming 'ax' is your UIAxes handle and 'pack' is your
battery pack object
packChart = batteryChart(ax, pack);If you are looking to visualize specific parameters over time (like state of charge or temperature), consider using data logging techniques. The batterySimulationLog object allows you to capture simulation data that can then be plotted independently of the BatteryChart. For more information on this object, please refer to
Regarding your point about cell creation syntax, you are correct that the line:
app.Cell = simscape.battery.Cell(....)
is incorrect. Instead, you should define your cell object as follows:
geometry = batteryPouchGeometry; % or any appropriate geometry function app.Cell = batteryCell(geometry);
This change ensures that you're correctly instantiating a batteryCell object with the specified geometry.
If further challenges arise, feel free to ask for more specific guidance!
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.
Categories
Find more on Tuning Goals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!