Update bar chart UIAxes from EditField value without deleting older plot
8 views (last 30 days)
Show older comments
Hi. I am building an app where whenever I enter value in EditField it automatically plot a bar chart in UIAxes.
I manage to do that. However, I would like to continously update the bar chart wehenever new value enter into the EditField without deleting the older plot (bar chart).
Appreciate your help on this matter.
14 Comments
Simon Chan
on 10 May 2023
Edited: Simon Chan
on 10 May 2023
The following may be a workaround for you, noticed that someone may have a better solution.
When you define the UIAxes, set its user data to an empty array
set(app.UIAxes,'UserData',[]);
When you enter a new EditField, the UserData will be updated.
In this case, it simply overwrite the previous plot with a new plot and hence no need to use hold on and hold off.
However, if you use the same axis for a completely new plot, you need to set the UserData of the UIAxes to [] somewhere otherwise the old data will not be clear.
Y = [app.TotalAllowableWeighttonEditField.Value;app.ActualTotalWeighttonEditField.Value];
app.UIAxes.UserData = [app.UIAxes.UserData;Y'];
bar(app.UIAxes,X, app.UIAxes.UserData,'grouped','FaceColor','flat');
Accepted Answer
Cris LaPierre
on 10 May 2023
Edited: Cris LaPierre
on 10 May 2023
The way I would approach this in App Designer is to indeed recreate the entire plot each time. I would create an app property to capture the edit field values. Everytime a new value gets added to the edit field, it gets appended to this array.
Then when you plot the data in the bar graph, plot all the values, not just the new ones. The important thing is that your X values indicate the group(s) correctly. The first time, your X value is 1, the second time it is [1 2], etc. You can update the xticklabels to be what you want, but you haven't shared how you determine what they are, so for now, I use group number.
properties (Access = private)
Y
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
app.Y = [app.Y; app.TotalAllowableWeighttonEditField.Value, app.ActualTotalWeighttonEditField.Value];
bar(app.UIAxes,1:size(app.Y,1), app.Y,'grouped','FaceColor','flat');
legend(app.UIAxes,"Allowance","Actual")
end
end
You can add bar height labels by following this example: https://www.mathworks.com/help/matlab/ref/bar.html#mw_735ee38d-9d9f-40cd-b02a-8266af7184d9
5 Comments
Cris LaPierre
on 15 May 2023
That error has to do with your input values. Please share what each edit field value is when you get this error.
I suspect that is because your X values are char arrays. They need to be strings to avoid this error. Update this line of code to the following:
app.X = [app.X; string(app.LabelEditField.Value)];
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer 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!