How to do polyfit in loop format, for the selected plots.
6 views (last 30 days)
Show older comments
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 Comments
Steven Lord
on 15 Oct 2024
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.]
Answers (1)
dpb
on 15 Oct 2024
Edited: dpb
on 15 Oct 2024
...
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 Comments
dpb
on 15 Oct 2024
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.
See Also
Categories
Find more on Introduction to Installation and Licensing 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!