Error using fit: Index in position 1 is invalid. Array indices must be positive integers or logical values.
3 views (last 30 days)
Show older comments
Vincent Wohlleben
on 28 Jul 2022
Commented: Vincent Wohlleben
on 28 Jul 2022
Hello,
I am facing a problem with the fit function in Matlab R2020a:
The following code is part of a script that produces a spline fit for raw sideforce data with respect to the slipangle data of a tire. The einlesen function takes the chosen set of data and creates a struct for the different channels (sideforce in field 8 and slipangle in field 2).
[FILES, fpath] = uigetfile({'*.tdms'},'tdms file wählen','Multiselect','On');
fname = FILES{1};
D = einlesen([fpath fname]); % Creates struct D with all channels and their respective data sorted by name
xdata(1,:) = -D(8).values; % Slipangle data
ydata(1,:) = D(2).values; % Sideforce data
fitting = fit(xdata',ydata','smoothingspline','SmoothingParam',0.1); % Error line
y(1,:) = feval(fitting,xdata(1,:)');
When I run the script, it produces the following error while referring to the fitting line:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
The strange thing about it is that, when I load the dataset without using einlesen to transform it into a struct and I therefore have the different data channels as distinct variables, everything works. There seems to be a problem with me getting the data out of the struct and feeding it into the fit function but I cannot understand why that is because the data is exactly the same. I am not even indexing either, it is just input data for the function.
Does anybody have an idea of what's wrong with my using of the fit function? Help would be greatly appreciated!!
Best regards,
Vincent
2 Comments
Walter Roberson
on 28 Jul 2022
einlesen is not a matlab function that I can find.
I thought for a moment that it might be a translation of "load" but load returns scalar structures when it returns a structure at all.
Accepted Answer
Steven Lord
on 28 Jul 2022
I'm guessing you've created a variable named fit that's preventing MATLAB from calling the fit function. You can check this using the which function.
which fit % Shows the function, since the variable doesn't exist yet
fit = 42;
which fit % Does not show the function, since the variable "shadows" it
Rename the variable. You may also need to modify the code (if the line that creates the variable is in your script or function) to create it with the new name.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!