Update bar chart UIAxes from EditField value without deleting older plot
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
Simon Chan
2023-5-10
编辑:Simon Chan
2023-5-10
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');
采纳的回答
Cris LaPierre
2023-5-10
编辑:Cris LaPierre
2023-5-10
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 个评论
Cris LaPierre
2023-5-15
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)];
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!