readtable with fixed column width from a txt file
Show older comments
Hi there,
So I have a text file that is generated from another computational code and looks like this:

What I'd like to do is to import this file into Matlab with proper data structure and pretty much use the key value pair to perform quick query about the "Matrix Eqn" with given (Node, DOF) keys. I realized readtable() is potentially a good candidate to import the file. But when I use the command, Matlab (I'm using Matlab R2014b) returns a warning like this:
>> T = readtable('Stiff_HB.mapping','FileType','text');
Warning: Variable names were modified to make them valid MATLAB identifiers.
And the table T looks like this:
>> T.Properties
ans =
Description: ''
VariableDescriptions: {'Original column heading: 'Matrix Eqn Node DOF''}
VariableUnits: {}
DimensionNames: {'Row' 'Variable'}
UserData: []
RowNames: {}
VariableNames: {'MatrixEqnNodeDOF'}
A possible solution for this is to first import the text data file into excel and set the column width constant, Excel will then divide the data set into a 3-column like structure. By calling readtable again with the Excel file as an input argument, the table would recover all three properties that I want. The warning shown below is because the property name "MatrixEqn" originally has a whitespace. However, reading from Excel file is way slower than reading from text file.
>> T = readtable('Stiff_HB_mapping.xlsx');
Warning: Variable names were modified to make them valid MATLAB identifiers.
>> T.Properties
ans =
Description: ''
VariableDescriptions: {'Original column heading: 'Matrix Eqn'' '' ''}
VariableUnits: {}
DimensionNames: {'Row' 'Variable'}
UserData: []
RowNames: {}
VariableNames: {'MatrixEqn' 'Node' 'DOF'}
Also, there are possibly thousands of such text files for processing and it's really not a good idea to introducing an excel interface for data importing. Thus, I wonder if it is possible to simply get the job done in Matlab solely. Thanks in advance for any tips and suggestions.
3 Comments
Stephen23
on 4 Sep 2017
Is it necessary to use readtable for this? It would be trivial to use textscan with its MultipleDelimsAsOne option.
Fu-Rui Xiong
on 4 Sep 2017
Walter Roberson
on 5 Sep 2017
It looks to me as if you have tab delimited fields.
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Conversion 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!