# How do I use specific columns from a csv file

10 views (last 30 days)

Show older comments

Hi, I have a csv file and I am trying to use specific columns to input into a function. Below is the code that I have so far where FVC and Height are the names of two of the columns from my csv data.

%% Load data

fvc = readtable("fvc-2022.csv");

%% Fit linear model

fitLinearModel(FVC, Height)

btw, 'fitLinearModel' is the function that I am using and I have attached the csv data set. I know that the 'FVC, Height' part of the code is incorrect so I was wondering what the right code is to add in here.

Thanks.

##### 0 Comments

### Accepted Answer

Star Strider
on 5 Sep 2022

I am not certain what you want to regress.

Perhaps one of these —

fvc = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1117350/fvc-2022.csv')

mdl = fitlm(fvc, 'Weight~FVC+Height')

mdl = fitlm(fvc, 'FVC~Height')

mdl = fitlm(fvc, 'Height~FVC')

Make appropriate changes to get the desired result.

See the documentation on fitlm for details, and the links in See Also for related functions and documentation.

.

##### 2 Comments

Star Strider
on 5 Sep 2022

‘I also have a premade function called 'fitLinearModel' which uses two vectors and finds the intercept, slope and residual variance.’

‘What I want to do is use two columns from the csv file as the two vectors in that premade function but I'm not too sure on how to call it. I believe right now that the proper coding is the following...’

The way you’re calling it is correct. However, it has three outputs, so you need to include them in the function call, for example:

[intercept, slope, resVar] = fitLinearModel(fvc.FVC, fvc.Height)

Otherwise, the call to it (without specifying the outputs) will return only the ‘Intercept’ output. Also, it’s ineffecient to do the regression twice as your function currently does. Use polyval with the returned polynomial and the ‘x’ vector, then calculate the residuals and their variance from that.

FWIW, using fitlm is likely easier to use and more robust. It does all the statistical calculations internally, and reports the results. If you want to get the residual variance from it yourself, use the predict function (there are several, the one I linked to appears to be the appropriate one, although MATLAB will likely choose the correct one given the correct arguments), then calculate the residuals and their variance from that.

.

### More Answers (1)

Image Analyst
on 5 Sep 2022

##### 6 Comments

Image Analyst
on 5 Sep 2022

Try this:

clc; % Clear the command window.

close all; % Close all figures (except those of imtool.)

clear; % Erase all existing variables. Or clearvars if you want.

workspace; % Make sure the workspace panel is showing.

format long g;

format compact;

fontSize = 15;

t = readtable('fvc-2022.csv')

% Fit line through Heights

x = t.FVC;

y = t.Height;

[intercept, slope, resVar] = fitLinearModel(x,y)

% Fit line through Heights

x = t.FVC;

y = t.Weight;

[intercept, slope, resVar] = fitLinearModel(x,y)

function [intercept, slope, resVar] = fitLinearModel(x,y)

%intercept

P = polyfit(x,y,1);

intercept=P(2);

%slope

slope=P(1);

%resVar

A = [ones(numel(x),1), x(:)];

b = y(:);

c = A\b;

format long

resVar=var(y-(c(1)+c(2)*x))*(numel(x)-1)/(numel(x)-2);

end

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!