Update bar chart UIAxes from EditField value without deleting older plot

10 次查看(过去 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
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');
FEH
FEH 2023-5-10
@Simon Chan Its working!!! Thank you so much!
But do you have any idea how can I make the Xlabel following the new frame no enter in the edit field?
E.g. first set of EditField, the frame no editfield is 0 to 17. Next frame is 17 to 72.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
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
  5 个评论
FEH
FEH 2023-5-15
Dear @Cris LaPierre, sorry to ask again.
I have write a code as you said.
app.Y = [app.Y; app.TotalAllowableWeighttonEditField.Value, app.ActualTotalWeighttonEditField.Value];
app.X = [app.X; app.FrameEditField.Value];
bar(app.UIAxes, app.Y,'grouped','FaceColor','flat');
legend(app.UIAxes,"Allowance","Actual")
xticklabels(app.UIAxes,app.X)
However, when I try to plot the bar more than 3 times, the error "Dimensions of arrays being concatenated are not consistent" appear at line app.X = [app.X; app.FrameEditField.Value];.
Cris LaPierre
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 CenterFile Exchange 中查找有关 Visual Exploration 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by