Clear Filters
Clear Filters

Update bar chart UIAxes from EditField value without deleting older plot

14 views (last 30 days)
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
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');
FEH
FEH on 10 May 2023
@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.

Sign in to comment.

Accepted Answer

Cris LaPierre
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
  5 Comments
FEH
FEH on 15 May 2023
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 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)];

Sign in to comment.

More Answers (0)

Categories

Find more on Visual Exploration 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!