MATLAB Answers

Read table in .txt to MATLAB

24 views (last 30 days)
Karl Zammit
Karl Zammit on 17 Apr 2021 at 13:52
Commented: Karl Zammit on 19 Apr 2021 at 13:40
I have the following .txt fil and wish to save the columns seperately for further polar extrapolation. However, the following code is resulting in some funny values.
I appreciate any help. I think I am reading the data wrong as I dont fully understand the concept.
Thanks in advance.
%Read data file: Drag and Lift Coeff.
saveFlCdCl = 'Save_CdCl.txt'; %File name
finputCdCl = fopen(saveFlCdCl); %Open file for reading
dataBuffer = textscan(finputCdCl, '%f %f %f ', 'CollectOutput', 1, ... %Read data from file
'Delimiter', '', 'HeaderLines', 12);
%Get values
PolarAoA=dataBuffer{1}(:,1);
PolarCl=dataBuffer{1}(:,2);
PolarCd=dataBuffer{1}(:,3);
fclose(finputCdCl); %Close file

Accepted Answer

Walter Roberson
Walter Roberson on 19 Apr 2021 at 11:03
%Read data file: Drag and Lift Coeff.
saveFlCdCl = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/586711/Save_CdCl.txt'; %File name
t = readtable(saveFlCdCl, 'readvariablenames', false, 'headerlines', 12);
t.Properties.VariableNames = {'alpha', 'CL', 'CD', 'CDp', 'CM', 'Top_Xtr', 'Bot_Xtr'};
t(1:5,:)
ans = 5×7 table
alpha CL CD CDp CM Top_Xtr Bot_Xtr _____ _______ _______ _______ _______ _______ _______ -10 -0.6476 0.02064 0.01596 -0.1001 0.9787 0.0217 -9.5 -0.583 0.01903 0.0142 -0.1033 0.9748 0.0229 -9 -0.5143 0.01752 0.01241 -0.1069 0.9722 0.0245 -8.5 -0.4585 0.01575 0.01051 -0.1079 0.9641 0.0263 -8 -0.3959 0.01462 0.00923 -0.1096 0.9573 0.0282
t(end-4:end,:)
ans = 5×7 table
alpha CL CD CDp CM Top_Xtr Bot_Xtr _____ ______ _______ _______ _______ _______ _______ 18 1.4983 0.09514 0.09032 -0.0493 0.0172 1 18.5 1.4798 0.10449 0.09981 -0.0514 0.0166 1 19 1.4728 0.11262 0.10816 -0.0538 0.0161 1 19.5 1.465 0.12103 0.11678 -0.0568 0.0156 1 20 1.4565 0.12969 0.12561 -0.0602 0.0151 1
  1 Comment
Karl Zammit
Karl Zammit on 19 Apr 2021 at 13:40
Needed the seperate columns but got to it in the end thanks to your answer. Much appreciated

Sign in to comment.

More Answers (1)

David Hill
David Hill on 17 Apr 2021 at 14:07
t=readtable('Save_CdCl.txt');
alpha=t.alpha;
names=t.Properties.VariableNames;
for k=1:length(names)
writetable(t(:,k),names{k});
end
  1 Comment
Karl Zammit
Karl Zammit on 19 Apr 2021 at 10:27
This shows up for your solution

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by