Reading unformat text file with strings and numbers
2 views (last 30 days)
Show older comments
Hello,
I am trying to read an unformated text file like this:
name units mean std min max
pi_turret deg 0.00377036 1.00158 -3.3625 3.94779
yw_turret deg -0.00470344 0.0664195 -0.760036 0.524774
fx_mooring kN -884.197 775.484 -4597.79 2213.77
fy_mooring kN -530.147 339.538 -2607.62 1421.66
fz_mooring kN -10503.4 1160.45 -17819.9 -3729.37
mx_mooring kN.m 13821.2 7127.5 -7565.57 49325.8
my_mooring kN.m -20736 13770.1 -86428.6 28767.7
mz_mooring kN.m 80.9098 765.169 -5632.16 13517.5
offset_turret - 2.70972 1.23327 0.0209747 7.59684
fx_mooring_lua - -885.952 722.077 -4527.8 1812.17
fy_mooring_lua - -527.852 287.204 -2457.86 1189.34
fz_mooring_lua - -10508.9 1159.42 -17810.1 -3837.86
fz_TurInertia_lua - -10508.7 1258.69 -18761.3 -1889.77
fz_TurInertia_KG_lua - -10507.4 1258.68 -18756.7 -1881.07
fhor_mooring - 1127.58 629.278 2.8272 4966.67
mx_mooring_fairleads - 1052.92 681.499 -3639.58 9207.27
my_mooring_fairleads - -1862.32 1529.37 -11006.9 4167.52
mhor_mooring_fairleads - 2336.6 1385.85 4.60501 12896.2
tfair_v13_1 N 1.20319e+06 166417 -343185 2.33059e+06
I tried to use the text scan and readtable functions, as presented below
fileID = fopen(fullfile(simdir,textfile),'r');
file = fullfile(simdir,textfile)
formatSpec = '%s%s%f%f%f%f';
startRow = 2;
delimiter = '\t';
dataArray = readtable(file,'Format',formatSpec,'Delimiter', delimiter);
%dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue', NaN, 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID);
However, in both options I got all the data in the first collumn of the output cell and the five remaining collums empty. Thus, I beliave I am making some mistake or forgetting some input setting. I only need the numbers, so an option could be to skyp the first 2 collums when I imported it. Any ideas about ? Thanks
3 Comments
VBBV
on 30 Jul 2024
@Alex you could also use MultipleDelimsAsOne argument to get the same result. However, in your code the delimiter is given as tab '\t', you need to give it as space as below
file = 'data.txt'; %fullfile(simdir,textfile)
formatSpec = '%s%s%f%f%f%f';
startRow = 2;
delimiter = "space"; % specify this as space
dataArray = readtable(file,'Format',formatSpec,'Delimiter', delimiter,'MultipleDelimsAsOne',1)
format long G
dataArray_num = dataArray{:,3:end} % table only with numbers
Accepted Answer
Stephen23
on 30 Jul 2024
Edited: Stephen23
on 30 Jul 2024
In lieu of a sample data file provided by the OP I created my own...
Lets try importing it using READTABLE and a few options:
T = readtable('mydata.txt', 'LeadingDelimitersRule','ignore', 'ConsecutiveDelimitersRule','join')
format long G
M = T{:,3:end}
More Answers (0)
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!