Matlab: Working for-loop breaks in parfor while fitting curves

191 Views Asked by At

Hoping you may be able to assist me with this error. I am running some code to fit curves to ages using a cross validation regime. I iterate the curve fitting 1000 times to assess the best fit.

I define my models as:

linear_ft = fittype({'x', '1'});
monotonic_ft= fittype({'-1/x', '1'});
quadratic_ft = fittype('poly2'); 

I then run the following to iterate through different selections of data splitting, recording the residuals following the curve fit...

Data = randn(4,300,10,10);
Ages = randn(300,1);

for thisDim1 = 1:4
    for thisDim2 = 1:10
        for thisDim3 = 1:10
            for nIts = 1:1000
                RandomOrder = randperm(300,300);
                Fit_Subs = RandomOrder(1:length(Ages)/2); % Take random subs to fit to
                Test_Subs = RandomOrder(length(Ages)/2+1:300); % Take random subs to test fit to

                Fit_Data = squeeze(Data(thisDim1,Fit_Subs,thisDim2,thisDim3)); % Take data to fit to
                Test_Data = squeeze(Data(thisDim1,Test_Subs,thisDim2,thisDim3)); % Take data to test fit

                Fit_Ages = Ages;
                Fit_Ages(Fit_Subs) = []; %Take ages of Fit Subs only
                Test_Ages = Ages;
                Test_Ages(Test_Subs) = []; % Take ages of Test Subs only

                Nsubs = (length(Ages)/2);

                % Model Data using Curves
                fFit_Lin = fit(Fit_Ages,Fit_Data',linear_ft);
                fFit_Mon = fit(Fit_Ages,Fit_Data',monotonic_ft);
                fFit_Quad = fit(Fit_Ages,Fit_Data',quadratic_ft);

                % Fit Modelled Data to Test Data
                tFit_Lin = fFit_Lin(Test_Ages);
                tFit_Mon = fFit_Mon(Test_Ages);
                tFit_Quad = fFit_Quad(Test_Ages);

                % Calculate Median Residual
                Lin_Med_Resid(nIts) = median(tFit_Lin - Test_Data');
                Mon_Med_Resid(nIts) = median(tFit_Mon - Test_Data');
                Quad_Med_Resid(nIts) = median(tFit_Quad - Test_Data');

            end
        end
    end
end

If you run this with the fourth loop (nIts) as a for-loop it will run. If you run it as a parfor-loop it won't stating the error:

Error using fit>iFit (line 264) The name 'lower' is not an accessible property for an instance of class 'llsqoptions'.

Error in fit (line 108) [fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...

Does anyone have any idea how to fix this? I would be most grateful for any advice!!

Thanks,

Ben

1

There are 1 best solutions below

1
informaton On BEST ANSWER

Try restarting MATLAB or typing clear all to see if it clears things up for you.

Your code works for me, but the parallel toolbox can be a bit finicky in my experience.