MATLAB: Linear regression on excel dataset

linear regressionStatistics and Machine Learning Toolbox

I am new in MATLAB and have tried to do a linear regression with the code:
>> filename = 'C:\Users\Troels\Dropbox\Analyse & Resultater\MATLAB\Danmark OX.xls';
>> ds = xlsread(filename) // the dataset "Danmark OX.xls" is printet
>> mdl = LinearModel.fit(ds)
Error using classreg.regr.TermsRegression/handleDataArgs (line 629) Y argument is required unless X is a dataset.
Error in LinearModel.fit (line 891) [X,y,haveDataset,otherArgs] = LinearModel.handleDataArgs(X,varargin{:});
Have also tried the code:
>> ds = dataset('XLSFile','C:\Users\Troels\Dropbox\Analyse & Resultater\MATLAB\Danmark OX.xls','ReadObsNames',true);
Warning: Variable names were modified to make them valid MATLAB identifiers.
> In @dataset\private\genvalidnames at 56
In @dataset\private\setvarnames at 40
In dataset.readXLSFile at 49
In dataset.dataset>dataset.dataset at 352
>> mdl = LinearModel.fit(ds);
Warning: Regression design matrix is rank deficient to within machine precision.
> In TermsRegression>TermsRegression.checkDesignRank at 98
In LinearModel.LinearModel>LinearModel.fit at 944
Am I on the right track in any of my 2 attempts? And can anyone tell me from the above what I do wrong?

Best Answer

mdl = LinearModel.fit(ds)
assumes, ds is a dataset (Your second approach) and the last column of ds is the response variable. Is the true in your case?
If you want to pass data as a matrix then you have to do so this way:
mdl = LinearModel.fit(X,y)
I urge you to read the documentation of LinearModel.fit to understand how to call it. This will save you a lot of time later on. There are plenty of examples there: