Fitting Excel data in Matlab

Hi, I have one question.
I have a CSV file that I want to fit the data in columns 30 and 33 and I wrote the fit function to draw fit plot and data plot, but I don't know how to convert CSV file columns to a vector and give it to the function as input.
The function code is shown below.
I need your help. Thank you
function y = fit(x0,y0)
X1 = [ones(length(x0),1) x0'];
b = X1\y0'
y = b(1) + x0*b(2)
slope = b(2)
intercept = b(1)
plot(x0,y0,'o')
hold on
plot(x0,y,'--r')

 Accepted Answer

Use the readmatrix or other appropriate function, then go from there:
D = readmatrix('student-mat.csv');
Note that ‘D’ is a (395x33) matrix, so you need to figure out what columns you want to use.

6 Comments

Thank you
but I use matlab R2017a and readmatrix doesn't work.
Can you say how to use it to transform column to vector in Matlab?
Use readtable (you might need to use detectImportData as well)
filename = 'student-mat.csv';
opt = detectImportOptions(filename);
D = readtable(filename, opt);
G3 = D.G3;
absences = D.absences;
Thank a lot
It worked.
Our pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!