How to do polyfit in loop format, for the selected plots.

6 次查看(过去 30 天)
Hi all, I have created an app, Let me give brief intro and the problem i'm facing. The app starts by entering the Number of classes value. If you entered value is 2 then,the tab group with 2 tabs named as classroom plus 1tab named as Fit will be generated. In each classroom tab click to enter input parameters button also be generated. After clicking the button, Classroom edit field(the value tells about the Classroom No.) with enter button will be genrated. After entering classroom number as "30" and click enter then, Number of columns edit field(the value tells about the bench columns in that class)with enter button is generated. After entering the No. of column value as "3" and click enter those many input boxes will generate with another enter button.After entering 1,2 and 3 values in each input boxes and click enter, Number of columns for each bench label, input boxes for the respective column and enter button is generated. After entering '2','2','1' in each input box and click enter. Those many Import excel buttons with null ui axes and a label as "select the suitable fit (choose Ye/No)" with Next button are generated. After importing the excel data the plane Uiaxes will be plotted with the imported data. After the plots are generated click 'next' button then below for each plot 'Yes', 'No' buttons and a label 'choose the fit' with linear fit and square fit buttons are genrated. " The problem I'm facing in the callback function of Yes button and Linear fit button". Requirement: The Yes buttons tells which plots has to take for the polyfit. After clicking '2' yes buttons in first column, '1' yes button in second column and '1' yes button in third column. This selection needs to be stored in one variable as '4'(out of 5 plots we have selected 4). Use that variable for looping the polyfit. I'm attaching the reference along with the question. It will be a big help. Thank you in advance.
  2 个评论
Steven Lord
Steven Lord 2024-10-15
Please show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
As stated I'm not sure what help you're looking for. [If you're asking us to implement all of what you described, that seems like a lot of work to ask of someone on MATLAB Answers.]
Vaibhavi Pallavaneni
编辑:dpb 2024-10-15
To be precise, My problem is at Yes button callback function. I'm trying to store the selected plots as numeric into a variable for further use. People can know my problem exactly if they review my app. That's why i have given every detail by attaching the file. As per u suggested, I'm attaching the code where i'm stuck. It will be helpful, if u can. Thank u in advance.
function YesbuttonPushed(app, tab, Classroom, m, n)
numBenches = app.BenchInputBoxes{Classroom}(m).Value;
app.YesSelected = zeros(numBenches,1);
end
function LinearFitbuttonpushed(app, tab, Classroom,m,n)
plotdata = app.YesSelected{Classroom}(m);
C = {'b','r','g','y','k'};
polyx = 0;
for i = 1: plotdata
polyp = polyx + polyfit(app.BoysCu_real(:,i),app.GirlsCu_real(:,i),1);
plot(app.Fitaxes,app.BoysCu_real(i,:),app.GirlsCu_real(i,:),strcat(C{i},'o'),'LineWidth',1);
end
polyA = polyp./plotdata;
plot(app.Fitaxes{Classroom}(m),app.BoysCu_real(i,:),polyval(polyA,app.BoysCu_real(i,:)),'k--');
end

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2024-10-15
编辑:dpb 2024-10-15
...
polyx = 0;
for i = 1: plotdata
polyp = polyx + polyfit(app.BoysCu_real(:,i),app.GirlsCu_real(:,i),1);
polyfit doesn't return a value but a vector of coefficients of nDegrees+1; you need to allocate room for those as
polyCoeff=zeros(numFits,nDegree+1);
where numFits is how many fits you're going to compute and nDegree is the degree of the fitted polynomial (1 in the above code is hardcoded; it would be better to make it a variable so could change if it became desireable).
Your loop goes over plotdata, so it would appear to be that numFits above would be the value of plotdata. Then
polyCoeff(i,:)=polyfit(app.BoysCu_real(:,i),app.GirlsCu_real(:,i),1);
would leave you with an array of coefficients.
But, if the intent is to fit each of these and plot the results, then it would make sense to call both polyfit and polyval inside the loop; otherwise you'll have to have another loop to evaluate each row of the coefficients array with the desired independent variable values vectors.
It is certainly far too much to expect folks to go digging through a complete complicated .mlapp file to learn the details of your code...paring it down to such specific pieces at least gives somebody something to comment on.
  4 个评论
dpb
dpb 2024-10-15
OBTW, the above array of coefficients is a 2D array; you might find it more convenient later on to store each set of coefficients in a cell array instead as the 2D array.
Vaibhavi Pallavaneni
@dpbThe variable is plotdata and as you mentioned above "Your loop goes over plotdata, so it would appear to be that numFits above would be the value of plotdata." the value of numfits should be number of yes buttons selected. I'm finding difficulty in that area. I hope u get an idea about the issue. Please Help!!! Thank u in advance

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by