Clear Filters
Clear Filters

Readtable produces a 1x 1 table full of strange characters

1 view (last 30 days)
I am loading tables from several large nonuniform .txt files delaminated with spaces. I'm using readtable and most of the tables get loaded without an issue but some files generate a 1x1 table full of stange characters. The code I'm using to load:
filename = 'filename.txt'
opts = detectImportOptions(filename);
opts = setvartype(opts,'char'); % or 'string'
Table = readtable(filename,opts);
I can't tell what is causing some files to be read like this.
  1 Comment
Les Beckham
Les Beckham on 17 Oct 2023
If you provide a sample file that causes this issue you will be more likely to get a good answer. Use the paper clip icon in the INSERT section of the comment editor toolbar to attach it.

Sign in to comment.

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 17 Oct 2023
If some of the imported data looks strange, that means the setvartype option is not appropriate for the data to be imported. See these two examples:
filename = 'DATA_2_MIX.txt';
opts = detectImportOptions(filename);
opts = setvartype(opts,'double'); % 'single','char' or 'string'
Table = readtable(filename,opts)
Table = 9×15 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ 0.25974 0.25264 0.24121 0.23309 0.22278 0.21093 0.20217 0.19373 0.18444 0.17568 0.16822 0.16053 0.15544 0.14992 0.14689 0.29785 0.28817 0.28102 0.27025 0.26136 0.24949 0.23689 0.22539 0.21672 0.20579 0.19648 0.18955 0.18458 0.18057 0.17556 0.2846 0.27922 0.27212 0.2631 0.2525 0.23936 0.22876 0.21848 0.2103 0.20247 0.1947 0.18774 0.17816 0.17506 0.16893 0.2776 0.27279 0.26541 0.25595 0.24572 0.23601 0.22354 0.21099 0.20298 0.19335 0.18475 0.17964 0.1746 0.16754 0.16517 0.25408 0.24673 0.24188 0.23601 0.22816 0.21799 0.20633 0.19619 0.18844 0.18086 0.17347 0.16587 0.15909 0.15253 0.14945 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
filename = 'DATA_2_MIX.txt';
opts = detectImportOptions(filename);
opts = setvartype(opts,'char'); % 'single','char' or 'string'
Table = readtable(filename,opts)
Table = 9×15 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 _________________________________________________________________________________________ _____________________ _____________________ _____________________ _____________________ _____________________ _____________________ _____________________ _____________________ _____________________ _____________________ _____________________ _____________________ _____________________ _____________________ {'0.259743404425469' } {'0.252643855530589'} {'0.241205819807817'} {'0.233089191009609'} {'0.222784020513450'} {'0.210927448505497'} {'0.202169889641793'} {'0.193729347215780'} {'0.184436398887280'} {'0.175684122975396'} {'0.168219717191859'} {'0.160525922324245'} {'0.155440426354408'} {'0.149916604634160'} {'0.146893741622443'} {'0.297854342663850' } {'0.288172051206166'} {'0.281019918694305'} {'0.270247013950806'} {'0.261355440324070'} {'0.249486303896369'} {'0.236886119627021'} {'0.225389381588091'} {'0.216717535994283'} {'0.205793684256531'} {'0.196480509491147'} {'0.189551047144315'} {'0.184576686428076'} {'0.180571819906026'} {'0.175561769492479'} {'0.284602873105606' } {'0.279224016862728'} {'0.272118987871460'} {'0.263096390401687'} {'0.252497953375377'} {'0.239358157063720'} {'0.228763432499433'} {'0.218479075198721'} {'0.210303995092566'} {'0.202465145758614'} {'0.194698356850316'} {'0.187737865062544'} {'0.178156868000645'} {'0.175057048903746'} {'0.168933685968333'} {'0.277601478441753' } {'0.272786361862176'} {'0.265405214945193'} {'0.255951310627497'} {'0.245721182613658'} {'0.236012222908688'} {'0.223544700698844'} {'0.210991381941889'} {'0.202982908946018'} {'0.193353953141278'} {'0.184745969704785'} {'0.179641384457305'} {'0.174600038577711'} {'0.167538615828359'} {'0.165169383208301'} {'0.254081846533581' } {'0.246732234329187'} {'0.241882238538824'} {'0.236007408670032'} {'0.228163960939587'} {'0.217985572821551'} {'0.206328464016468'} {'0.196193300128235'} {'0.188438755760450'} {'0.180860676378742'} {'0.173469902678243'} {'0.165865170385420'} {'0.159090426846049'} {'0.152534572227508'} {'0.149451152497086'} {'Here is the list of Arduino sensor kits and battery packs.' } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {'(1) Digi-Key Part Number: 1597-103030375-ND, Price: $28.50 --> 10 sets $285.00 (OK)' } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {'(2) Digi-Key Part Number: 4411-CZ0010D-ND, Price: $115.93 --> 10 sets $976.23 (OK)'} {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {'(3) Digi-Key Part Number: 1597-1103-ND, Price: $54.90 --> 5 sets $274.50 (OK)' } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char } {0×0 char }

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!