Importing Text File with Different Row Types

3 次查看(过去 30 天)
This may be a simple question but I had trouble phrasing it to find a direct answer from the help guide. So I am looking to import text files that have different row types similar to this (headers were added to show that columns are not the same all the way down):
% function comp comp # comp type group #
define PART01 0001 01 04
define PART02 0002 01 04
define PART03 0003 05 10
% function comp # test 1 test 2
testtype 0001 111 112
testtype 0002 121 122
testtype 0003 131 132
Normally I could take this into excel and seperate this into two parts for an easier import, but I need to do this for several files so I am trying to automate it.
My initial thought was to import the file with blanks being treated as 0, separate the tables like this, and fix the columns afterwards. It works but is there a more efficient to do this?
T1 = table(table.function=='define',:);
T2 = table(table.function=='testtype',:);

采纳的回答

Walter Roberson
Walter Roberson 2022-3-25
编辑:Walter Roberson 2022-3-25
If you use detectImportOptions() with 'ExpectedNumVariables' set to the maximum column, then you can (if necessary) also use setvaropts() to set the FillValue property for each variable, which will go along with the default 'MissingRule' option value 'fill'
In practice you might be able to just
readtable(Filename, 'ExpectedNumVariables', 5)
... but beware that if you do that then the second column will probably all be interpreted as character (unless it happens to start with enough rows that are numeric... in that case, readtable() might guess that it is intended to be numeric.)
When you start getting into more complex parsing situations, and you have enough memory, then it turns out that a lot of the time it becomes easier to read the entire file as text, and then use text processing techniques, possibly involving multiple passes with regexp(), or possibly detecting boundaries and doing substring extraction and using textscan() on the resulting character vectors.
  1 个评论
Madison Padilla
Madison Padilla 2022-3-25
Thanks for the response, I hadn't seen ExpectedNumVariables or regexp() before so that's good to know. I have to import more complicated inputs after these sets so I'll have to check out regexp and textscan soon enough.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by