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에서 Labels and Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!