Why a linear model cannot be plotted in app designer?

Hi,
I am using fit(x, y, 'poly1') in app designer.
a = linspace(0,10,10)'
b = linspace(0,20,10)'
[fitline,gof] = fit(a, b, 'poly1' )
plot(app.name, fitline, a, b)
When I try to plot the linear model and the vectors a and b I am getting the following error:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
I tried it the same in my commad window and there was no error there. Any idea what is causing it in app designer?
Thanks
Hugo

 채택된 답변

Adam Danz
Adam Danz 2020년 8월 19일
편집: Adam Danz 2023년 9월 18일
This issue has been fixed in MATLAB R2023b
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)
Workaround prior to R2023b: create plot externally, then copy to AppDesigner axes
Prior to R2023b, plotting a cfit object is not supported with uifigures (e.g. AppDesigner; see Graphics Support in App Designer). Even if it were supported, for whatever reason there is no way to specify an axis handle when plotting a cfit object, although that problem isn't difficult to solve.
For more info see
Workaround: You can plot the fit object in an external, temprary figure, copy its content to the App's axes, and then delete the temporary figure.
% Create uifigure and uiaxes to mimic AppDesigner
app.UIFigure = uifigure();
app.UIAxes = uiaxes(app.UIFigure);
% Plot cfit object externally.
a = linspace(0,10,10)';
b = linspace(0,20,10)';
[fitline,gof] = fit(a, b, 'poly1');
fig = figure('Visible','off'); % you may want to set this to "on" to see what's happening
ax = axes(fig);
h = plot(fitline,a,b);
% Copy content of temp axes to app designer
hApp = copyobj(h, app.UIAxes);
% You'll need to recreate the legend since it can't be independently copied
lh = legend(app.UIAxes);
% Delete temp figure
delete(fig)

댓글 수: 7

Hi Adam.
Thank you for the references and the steps to solve my problem. It worked just fine.
BR.
Glad I could help!
Thanks @Adam Danz for the lifesaving solution!!, but I am facing a small problem with this.
It creates a new uifigure, and I can close it but the current app is no more the uifigure so my code breaks wherever I have used uigifure elsewhere. Can you tell me how to make/maintain the app ui as the uifigure after copying the plot to my desired axes?
@Mohd Aaquib Khan, if you can update MATLAB to use R2023b, the most recent release, the problem is fixed and there's no need to use the workaround. I've updated my answer to reflect this change.
To address your question, I'm not sure I understand. Do you mean that your uifigure is no longer current? If so, try using set(0, 'CurrentFigure', app.UIFigure) where the 3rd input is your figure handle. This should be called within the callback function that implements the workaround.
Thanks Adam, so I can update to fix it.
No, my problem is not to make it current.
If I am not wrong, you are creating a uifigure window and plotting the curve in it, but the app by default is also uifigure. So once you create the new uifigure the old uifigure is not accessible. I had some buttondown functions and they dont work anymore.
And if I close the new window then I get error because the uifigure doesnt exist anymore.
Error using App/PlotButtonPushed
Invalid or deleted object
Thanks for the explanation @Mohd Aaquib Khan. It doesn't sound right. I'm creating a regular figure using the figure function as opposed to the uifigure function. Furthermore, the figure is generaged with the visible property set to off so you should never see the figure appear.
If you are still having trouble, you could share the full callback function so I can see what's going on.
Mohd Aaquib Khan
Mohd Aaquib Khan 2023년 9월 20일
편집: Mohd Aaquib Khan 2023년 9월 20일
Thank you for your time @Adam Danz,
Since you are investing your time, I have created a new question request for it so that you get some credit for it.
I am using pretty much the same code as you have provided.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 8월 19일

편집:

2023년 9월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by