readtable with fixed column width from a txt file
이전 댓글 표시
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
Stephen23
2017년 9월 4일
Is it necessary to use readtable for this? It would be trivial to use textscan with its MultipleDelimsAsOne option.
Fu-Rui Xiong
2017년 9월 4일
Walter Roberson
2017년 9월 5일
It looks to me as if you have tab delimited fields.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!