TextScan - flexible formatSpec string how to?
3 次查看(过去 30 天)
显示 更早的评论
Hey fellow "Matlabers"!
I have been struggling with this problem for quite a while and so far could not figure out a solution. Maybe someone here knows or have a suggestion.
The problem:
I am reading several .txt files, which contain all kind of data (strings, date format, numbers) and I only need to extract information of a few columns. The problem is that I need to ignore a certain amount of characters (marked as string) until I reach the first column that has data I need. For each file, the amount of characters can vary and therefore, I don't know how to specify on the formatSpec string that will be used in my textscan function. The number 59 is the value that varies; each file has a different number of characters to discard.
Example:
formatSpec = '%*59*s%10{dd/MM/yyyy}D%6{HH:mm}D%*10*s%*14s%10s%*8*s%*14s%10s%[^\n\r]';
textscan(fileID, '%[^\n\r]', startRow-1, 'ReturnOnError', false);
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'ReturnOnError', false);
Error message:
Error using textscan
Unable to read the DATATIME data with the format 'dd/MM/yyyy'. If the data is not a time, use %q to get
string data.
Any idea how can I automate this process?
Thanks in advance!
EDIT: I have added two .txt files as an example of what kind of data I am dealing with.
2 个评论
Stephen23
2015-5-20
Your description is great. All that is missing are a few sample text files, so that we can test out code on and see if it works. You can upload a few test files using the paperclip button, and not that you will need to push both the Choose file and Attach file buttons too.
It is much easier for us and also for you if we have real data to work with!
回答(1 个)
Walter Roberson
2015-5-20
ToSkip = 59;
formatSpec = ['%*', sprintf('%d', ToSkip), '*s%10{dd/MM/yyyy}D%6{HH:mm}D%*10*s%*14s%10s%*8*s%*14s%10s%[^\n\r]';
5 个评论
Walter Roberson
2015-5-20
The two sample files you provided can be handled by using
repmat('%*s',1,5)
as the data to skip.
By the way, are the columns possibly tab separated?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!