Problem using polyval/polyfit in App Designer R2022b

Hello i want to display a set of Input Data and Plot it.
After that i want to use polyfit/polyval on that Data and plot it also.
Some how this does not work in my App using App Designer ( in normal Matlab skript it worked)
here is an example code of it:
I used some Editfield to get diffrent Inputs
properties (Access = private)
Property % Description
Data
Range
PolyNom
function1
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: DisplayButton
function DisplayButtonPushed(app, event)
app.Data = [app.EditField.Value,app.EditField2.Value,app.EditField3.Value,app.EditField4.Value,app.EditField5.Value,app.EditField6.Value]
app.Range = 1:length(app.Data)
app.Range = app.Range';
plot(app.UIAxes, app.Data,'o')
hold(app.UIAxes);
end
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
app.PolyNom = polyfit(app.Range,app.Data,2);
app.function1 = polyval(app.PolyNom, app.Range);
plot(app.UIAxes2, app.function1);
I watched some Videos and they basicly do the same and it works, is this maybe a Version problem?
Thank you!

 Accepted Answer

Hi Florian,
are you using the Edit Filed (Numeric) Component, and not the Edit Field (Text), in your App?
Otherwise in app.Data you would be concatenating characters ['123456'] and not numbers [1,2,3,4,5,6].
Daniele

9 Comments

Hi Daniele,
yes i used the Numeric one and i also checked it using a raw number array. Didnt changed the problem.
Thanks anyways
Florian
It's weird. Because I used your code in MATLAB R2022b and I don't have any error.
What error do you have?
"Didnt changed the problem."
AFAIK you never told us what problem you have.
Oh you are completly right! i´m sorry
My Problem is/was that the polyval doesnt get plotted with no real Error it just dont show it.
According to your code,
plot(app.UIAxes2, app.function1)
the polyval is plotted called when you click on the axes where you plot the app.Data values.
If i do it like you and use only one button and put it all in one function it works for me aswell now. But i used a second button (poly) here to plot the polynom in the same fig and this doesnt work.
properties (Access = private)
Property % Description
Data
Range
PolyNom
function1
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: DisplayButton
function DisplayButtonPushed(app, event)
app.Data = [app.EditField.Value,app.EditField2.Value,app.EditField3.Value,app.EditField4.Value,app.EditField5.Value,app.EditField6.Value]
app.Range = 1:length(app.Data)
app.Range = app.Range';
plot(app.UIAxes, app.Data,'o')
hold(app.UIAxes);
% app.PolyNom = polyfit(app.Range,app.Data,3);
% app.function1 = polyval(app.PolyNom, app.Range);
%
% plot(app.UIAxes, app.function1);
end
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
app.PolyNom = polyfit(app.Range,app.Data,3);
app.function1 = polyval(app.PolyNom, app.Range);
plot(app.UIAxes, app.function1);
end
end
Put the debug break point in UIAxesButtonDown the first line. Launch your app, push the Poly button, see if MATLAB stops then execute step by step and see what it does.
According to your code:
function UIAxesButtonDown(app, event)
app.PolyNom = polyfit(app.Range,app.Data,3);
app.function1 = polyval(app.PolyNom, app.Range);
plot(app.UIAxes, app.function1);
end
you are plotting the polyval when you click on the axes, not when you click on the second button (poly).
If you want to plot the polyval when you click on your button (poly) you have to add that code to the ***ButtonPushed callback for your button.
of course... now it works thanks! i will check my "real" skript if there is the same silly mistake.
Thank you so much

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!