Polyfit matrix from import TXT file

4 views (last 30 days)
Tate Kafka
Tate Kafka on 6 Sep 2022
Commented: Rik on 7 Sep 2022
Hi, I am trying to import a TXT file containing 100 rows x 2 columns of data. I then want to take that data and polyfit it. However, what I have been trying so far receives the errror: Warning: Polynomial is not unique; degree >= number of data points. All of my matrices come out with correct data.
My current code:
% Import data
filename = 'A2Q2_input_a.txt';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename);
for k = [1:100]
D = (A.data(k, :));
domain(k) = D(1));
percent_err(k) = D(2);
% perform linear polyfit function
p_linear_R4 = polyfit(domain,percent_err,1);
  3 Comments
Tate Kafka
Tate Kafka on 7 Sep 2022
Yes i would like a single polynomial to be fitted to the entire matrix
Rik
Rik on 7 Sep 2022
Then the answer posted by KSSV should be what you need. If not, attach the file to your question and explain why it doesn't work.

Sign in to comment.

Answers (1)

KSSV
KSSV on 6 Sep 2022
% Import data
filename = 'A2Q2_input_a.txt';
T = readtable(filename);
x = T.(1) ;
y = T.(2) ;
% perform linear polyfit function
p_linear_R4 = polyfit(x,y,1);
yi = polyval(p_linear_R4,x) ;
figure
hold on
plot(x,y,'.b')
plot(x,yi,'r')

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!