Can sfit objects be plotted to specific axis in GUI (not a figure window)?
Show older comments
Hi,
I have been given a code that fits a surface to a 3D data set. My task is to add it to an existing GUI, that I have developed using App Designer in Matlab R2020a.
X=1:10; %Made-up data
Y=1:10;
Z=1:10;
n=(1:length(Z));
Ztop=(max(Z)+10).*ones(1,length(n));
plot3(app.Map, X(n),Y(n),Ztop,'.k','markersize',12); %plot datapoints
hold(app.Map, 'on')
options = fitoptions('Method','BiharmonicInterpolant');
[sf,gof] = fit([X', Y'],Z','biharmonicinterp',options); %create sfit obj
axis(app.Map,'equal')
caxis(app.Map,'auto')
%caxis([0 maxV]) ;
colormap(app.Map);
plot(app.Map, sf); %plot fitted surface - THAT'S THE ISSUE, IT WORKS UNTIL THIS LINE!
%shading interp
colorbar(app.Map);
%view(2)
hold(app.Map, 'off')
The code works just fine in a figure window but, when I try to display the output to a graphic axis (app.Map) integrated in the GUI, I get the following:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
I read the help for plot() and it seems that plot(ax,_) is the right format. However, I also tried another common format, just in case:
H = plot(sf, 'Parent', app.Map )
This returns a long list of errors. At this point, I think I need some guidance.
Thank you!
PS: one work-around might be plotting to a figure window, set with visibility 'off'. Then export the result to jpg and display it with imshow inside the GUI. But I would still like to know what I am doing wrong, if possible.
Accepted Answer
More Answers (2)
Geoff Hayes
on 16 Jun 2020
plot(sf, 'Parent', app.Map)
. When you try to plot as
plot(app.Map, sf);
7 Comments
Giorgio Menallo
on 16 Jun 2020
Geoff Hayes
on 16 Jun 2020
Which version of MATLAB are you using? What does it's documentation say about using plot for fit objects?
Giorgio Menallo
on 16 Jun 2020
Geoff Hayes
on 17 Jun 2020
It isn't clear to me why this wouldn't work. Perhaps try creating an example outside of your GUI. You can take any of the examples from fit and then try to plot as
plot(sf, 'Parent', gca )
Does that work or do you see the same errors?
Giorgio Menallo
on 19 Jun 2020
Geoff Hayes
on 19 Jun 2020
Gorigio - it may be that the UIAxes is not compatible with the fit object (see this older post https://www.mathworks.com/matlabcentral/answers/274880-app-designer-uiaxes-doesn-t-show-results-of-fitting-curve). It suggests that you need to do something similar to
load census;
f=fit(cdate,pop,'poly2');
x = 1700:2000;
y = feval(f,x);
plot(app.UIAxes,x,y)
but I'm not sure if that is valid for your use case.
Giorgio Menallo
on 20 Jun 2020
Adam Danz
on 18 Sep 2023
0 votes
Starting in MATLAB R2023b, provide the axis handle as the first input to plot the fit line in your app's axes.
plot(app.UIAxes, fitline, a, b)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!