How can I get the text instead of NaN when using import options for readtable?

2 次查看(过去 30 天)
Dear all,
I'm using readtable to import data from an open inventor file. The file has some delimited text and point cloud data as x,y,z coordinates that I need.
The file for example looks like this:
My code so far only separates all coordinates from the open inventor file.
To separate them I use readtable with following import options:
But now I want to:
  1. Import the open inventor file as a table (like I do already)
  2. Find a range that can be defined by a first/last row with a specified text
  3. Remove all NaN rows in this range to separate the coordinates
  4. Use finally a n-rows by 3-columns table to continue processing
My problem now:
If I convert the file to a table with the import options from above, I cannot search for the range boundaries I need, because every text element is converted to NaN. How can I get back the original text and search for the specified rows including text?
attached is a text file that is identical to the open inventor file. If you try to reproduce my code, please rename the extention to ".iv" to reproduce the same behaviour.
I'm using Matlab 2020b.
Thank you very much in advance!
  3 个评论
Florian Teriet
Florian Teriet 2020-11-27
Dear dpb,
thank you for your fast reply! Using fgetl is in my opinion to slowly for reading. I Solved it by calling readtable two times with different input options.
With Opts1 I get the whole line in one cell and then search for the target range by text.
With Opts2 I use my previous code, take my target range from Opts1 and delete all NaNs.
This is what I needed in this case.
If someone has a better idea do not hesitate and comment too. Thanks :)
Opts1 = detectImportOptions(DirectPath,'FileType','text','NumHeaderLines',0,'ExpectedNumVariables',3,'VariableNamingRule','preserve');
FileTable1 = readtable(DirectPath,Opts1);
FileArray1 = table2array(FileTable1);
StartIndex = find(ismember(FileArray1,StartText),1,'last');
EndIndex = find(ismember(FileArray1,EndText),1,'last');
Opts2 = detectImportOptions(DirectPath,'FileType','text','Delimiter',{',',' '},'PreserveVariableNames',true,'ExpectedNumVariables',3,'Range','A:C');%
FileTable12 = readtable(DirectPath,Opts2);
FileArray2 = table2array(FileTable12);
FileArray2(EndIndex:end,:,:)= [];
FileArray2(1:StartIndex,:,:)= [];
FileArray2 = rmmissing(FileArray2);
dpb
dpb 2020-12-16
I suspect you'd be surprised at relative timing between fgetl and calling detectImportOptions, readtable and table2array twice

请先登录,再进行评论。

回答(1 个)

Prashik Shende
Prashik Shende 2020-12-16

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by