problem with Fit function

6 views (last 30 days)
Gustaw Rutkowski
Gustaw Rutkowski on 11 Nov 2020
Edited: Abhaya on 21 Jan 2025
Hello, I have strange problem cause, on my computer fit function just desn't work. I thought that's my fault but code that I write compile properly in matlab online but does not in custom version, what's more I installed the curve fitting tool and ask for generating code matalb can;t run that program which was generated by it. Here is code, i would be gratful for pasting it into your matalb and say do you also get an error. My error is:
Error in createFit2 (line 18)
[xData, yData] = prepareCurveData( x, y );
Code generted by matlab:
function [fitresult, gof] = createFit2(x, y)
%CREATEFIT2(X,Y)
% Create a fit.
%
% Data for 'Dioda 1' fit:
% X Input : x
% Y Output: y
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 11-Nov-2020 23:05:36
%% Fit: 'Dioda 1'.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( 'exp1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Normalize = 'on';
opts.StartPoint = [117.535994401642 3.22582417353711];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'Dioda 1' );
plot( fitresult, xData, yData );
% Label axes
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
grid on

Answers (1)

Abhaya
Abhaya on 21 Jan 2025
Edited: Abhaya on 21 Jan 2025
The error 'Error in createFit2 (line 18) [xData, yData] = prepareCurveData( x, y );' indicates that MATLAB could not find appropriate prepareCurveData function while excuting 'createFit2' function. While I did not encounter any error when running the code, you can try the following steps to resolve the issue:
  • MATLAB Path Issues:Sometimes, MATLAB might not be able to access certain functions if they are not in the correct directory or if the toolbox is not added to the MATLAB path. You can try resetting the MATLAB path, which might help resolve any path-related issues.
restoredefaultpath;
rehash toolboxcache;
  • Input Data Format: Make sure that x and y are vectors of the same length, and that they are properly defined before calling the createFit2 function.
  • Conflicting Function or Variable Names: Ensure that there are no conflicting variable names in your workspace that could be overshadowing the prepareCurveData function. You can clear the workspace before running the createFit2 function.
clear all;
For more information, please go through the MATLAB documentation for 'prepareCurveData' function.

Categories

Find more on Fit Postprocessing in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!