How do I use specific columns from a csv file
이전 댓글 표시
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.
채택된 답변
추가 답변 (1개)
Image Analyst
2022년 9월 5일
0 개 추천
It's not finding your file. Your file lives in a different folder than your script, and it is also not on the search path. Use fullfile to specify the full path and base file name of your file.
댓글 수: 6
Declan
2022년 9월 5일
Image Analyst
2022년 9월 5일
MATLAB is case sensitive so FVC would be a different variable than fvc. Choose one or the other. Be consistent in your capitalization.
Declan
2022년 9월 5일
Image Analyst
2022년 9월 5일
What toolbox is fitLinearModel() in? Whatever it is, I don't have it. Plus you didn't define Height. What kind of model do you want to fit? Do you have a formula for it?
Declan
2022년 9월 5일
Image Analyst
2022년 9월 5일
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
카테고리
도움말 센터 및 File Exchange에서 Linear Predictive Coding에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!