Effacer les filtres
Effacer les filtres

Reading unformat text file with strings and numbers

2 vues (au cours des 30 derniers jours)
Alex
Alex le 30 Juil 2024
Commenté : Alex le 31 Juil 2024
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 commentaires
VBBV
VBBV le 30 Juil 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)
dataArray = 19x6 table
name units mean std min max ________________________ ________ __________ _______ ________ _______ {'pi_turret' } {'deg' } 0.0037704 1.0016 -3.3625 3.9478 {'yw_turret' } {'deg' } -0.0047034 0.06642 -0.76004 0.52477 {'fx_mooring' } {'kN' } -884.2 775.48 -4597.8 2213.8 {'fy_mooring' } {'kN' } -530.15 339.54 -2607.6 1421.7 {'fz_mooring' } {'kN' } -10503 1160.5 -17820 -3729.4 {'mx_mooring' } {'kN.m'} 13821 7127.5 -7565.6 49326 {'my_mooring' } {'kN.m'} -20736 13770 -86429 28768 {'mz_mooring' } {'kN.m'} 80.91 765.17 -5632.2 13518 {'offset_turret' } {'-' } 2.7097 1.2333 0.020975 7.5968 {'fx_mooring_lua' } {'-' } -885.95 722.08 -4527.8 1812.2 {'fy_mooring_lua' } {'-' } -527.85 287.2 -2457.9 1189.3 {'fz_mooring_lua' } {'-' } -10509 1159.4 -17810 -3837.9 {'fz_TurInertia_lua' } {'-' } -10509 1258.7 -18761 -1889.8 {'fz_TurInertia_KG_lua'} {'-' } -10507 1258.7 -18757 -1881.1 {'fhor_mooring' } {'-' } 1127.6 629.28 2.8272 4966.7 {'mx_mooring_fairleads'} {'-' } 1052.9 681.5 -3639.6 9207.3
format long G
dataArray_num = dataArray{:,3:end} % table only with numbers
dataArray_num = 19x4
1.0e+00 * 0.00377036 1.00158 -3.3625 3.94779 -0.00470344 0.0664195 -0.760036 0.524774 -884.197 775.484 -4597.79 2213.77 -530.147 339.538 -2607.62 1421.66 -10503.4 1160.45 -17819.9 -3729.37 13821.2 7127.5 -7565.57 49325.8 -20736 13770.1 -86428.6 28767.7 80.9098 765.169 -5632.16 13517.5 2.70972 1.23327 0.0209747 7.59684 -885.952 722.077 -4527.8 1812.17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Alex
Alex le 31 Juil 2024
Thanks !

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 30 Juil 2024
Modifié(e) : Stephen23 le 30 Juil 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')
T = 19x6 table
name units mean std min max ________________________ ________ __________ _______ ________ _______ {'pi_turret' } {'deg' } 0.0037704 1.0016 -3.3625 3.9478 {'yw_turret' } {'deg' } -0.0047034 0.06642 -0.76004 0.52477 {'fx_mooring' } {'kN' } -884.2 775.48 -4597.8 2213.8 {'fy_mooring' } {'kN' } -530.15 339.54 -2607.6 1421.7 {'fz_mooring' } {'kN' } -10503 1160.5 -17820 -3729.4 {'mx_mooring' } {'kN.m'} 13821 7127.5 -7565.6 49326 {'my_mooring' } {'kN.m'} -20736 13770 -86429 28768 {'mz_mooring' } {'kN.m'} 80.91 765.17 -5632.2 13518 {'offset_turret' } {'-' } 2.7097 1.2333 0.020975 7.5968 {'fx_mooring_lua' } {'-' } -885.95 722.08 -4527.8 1812.2 {'fy_mooring_lua' } {'-' } -527.85 287.2 -2457.9 1189.3 {'fz_mooring_lua' } {'-' } -10509 1159.4 -17810 -3837.9 {'fz_TurInertia_lua' } {'-' } -10509 1258.7 -18761 -1889.8 {'fz_TurInertia_KG_lua'} {'-' } -10507 1258.7 -18757 -1881.1 {'fhor_mooring' } {'-' } 1127.6 629.28 2.8272 4966.7 {'mx_mooring_fairleads'} {'-' } 1052.9 681.5 -3639.6 9207.3
format long G
M = T{:,3:end}
M = 19x4
1.0e+00 * 0.00377036 1.00158 -3.3625 3.94779 -0.00470344 0.0664195 -0.760036 0.524774 -884.197 775.484 -4597.79 2213.77 -530.147 339.538 -2607.62 1421.66 -10503.4 1160.45 -17819.9 -3729.37 13821.2 7127.5 -7565.57 49325.8 -20736 13770.1 -86428.6 28767.7 80.9098 765.169 -5632.16 13517.5 2.70972 1.23327 0.0209747 7.59684 -885.952 722.077 -4527.8 1812.17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by