Include Newline in Textscan

34 views (last 30 days)
Suganth Kannan
Suganth Kannan on 19 Jul 2016
Edited: dpb on 23 Jul 2016
I have the following function for auto-importing data from a tabular text file with delimiter " ".
function RMM_New_Bins = Import_RMM(filename, startRow, endRow)
delimiter = ' ';
if nargin<=2
startRow = 1;
endRow = inf;
end
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%[^\n\r]';
fileID = fopen(filename,'r');
textscan(fileID, '%[^\n\r]', startRow(1)-1, 'WhiteSpace', '', 'ReturnOnError', false);
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'ReturnOnError', false);
for block=2:length(startRow)
frewind(fileID);
textscan(fileID, '%[^\n\r]', startRow(block)-1, 'WhiteSpace', '', 'ReturnOnError', false);
dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'ReturnOnError', false);
for col=1:length(dataArray)
dataArray{col} = [dataArray{col};dataArrayBlock{col}];
end
end
fclose(fileID);
RMM_New_Bins = [dataArray{1:end-1}];
How do I include blank lines that are tabs in the RMM_New_Bins? I think it has to do with the textscan command.
  5 Comments
Suganth Kannan
Suganth Kannan on 20 Jul 2016
Edited: Suganth Kannan on 20 Jul 2016
@Stephen Cobeldick Have attached the sample file: "Sample.txt". Please click on it and highlight it in the browser to see how the file looks. Line 2 (blank) should not show up in the imported array. Only lines 1, 3, 4 (blank), 5, and 6 (blank) should show up.

Sign in to comment.

Answers (1)

dpb
dpb on 20 Jul 2016
Edited: dpb on 23 Jul 2016
Pretty bizzaro request: "Line 2 (blank) should not show up in the imported array. Only lines 1, 3, 4 (blank), 5, and 6 (blank) should show up" but whatever...
As it's a mixture of data can only have it as a (padded) character array or a cellstr array--I'll choose cellstr as it's much more flexible--
>> f=textread('sample.txt','%s','delimiter','\n','whitespace',''); % read full file as cellstr
>> f(cellfun(@isempty,f))=[]; % eliminate any totally blank rows
>> f % display result...
f =
' 4[A] 6[B] 7[C] '
'4[A] 0 6.23022 '
' '
'6[B] 6.23022 0 '
' '
>>
"How do I include blank lines that are tabs..."
ADDENDUM 2: Lines which contain tabs aren't really blank. Of course, to read them you have to use a way that doesn't treat them as delimiters or whitespace which the above does in reading each record as a cell string.
Then, as modified above, you can simply test for and eliminate those records that were, indeed, blank (and, hence, return TRUE for isempty on the record. Note that isempty on the array f is not the same thing as applying isempty to each member of the cell array which cellfun does).
Continuing after the diversion the previous illustration--
>> ~cellfun(@isempty,strfind(f,char(9)))
ans =
0
0
1
0
1
>> find(~cellfun(@isempty,strfind(f,char(9))))
ans =
3
5
>>
ADDENDUM
You can, of course, read the non-blank fields in the file with textscan, but it is impossible other than by parsing character-by-character to separate a blank field from a blank delimiter and so all you'll be able to get with it will be the non-missing data--
>> fid=fopen('sample.txt','r');
>> f=textscan(fid,repmat('%s',1,3),'collectoutput',1)
f =
{3x3 cell}
>> f{:}
ans =
'4[A]' '6[B]' '7[C]'
'4[A]' '0' '6.23022'
'6[B]' '6.23022' '0'
>>
  3 Comments
dpb
dpb on 23 Jul 2016
It dawned on me overnight it's actually an empty string if length()==0 so use the simpler test instead. I made a second update to the original answer incorporating same...

Sign in to comment.

Categories

Find more on Tables 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!

Translated by