MATLAB: Is MATLAB is automatically concatenating data from a text file

MATLABregexp

I have an input line of text that looks like this:
Band ID: 0 AD ID: 43 Scan ID: 0 LRT/HRT: 0 Valid Flag: 0
Note that the text file has a tab after each number except the last one.
I’ m using the following regexp command to split the string;
Data = regexp(LineText,’ +’,’split’);
Actual output:
Band ID: 0AD ID: 43Scan ID: 0LRT/HRT: 0Valid Flag: 0
Expected output:
Band ID: 0 AD ID: 43 Scan ID: 0 LRT/HRT: 0 Valid Flag: 0
Where all numerical values are in there own cell. Also note that if the tabs that occur after each number are only spacs, I get the required result.
I don’t believe I’ve seen the regexp function behave in this manner. Is this a case where regexp is choking on the tabs in the text file? Or is my regexp too generic?

Best Answer

What are you trying to achieve, extract the numbers? The space in the pattern ' +' won't match tabs actually, but '\s+' would (match spaces, tabs, any "white space"). If your goal were to extract numbers, you could go for
>> match = regexp(LineText,'\d+','match') ;
>> num = str2double(match)
num =
0 43 0 0 0
If you had a whole file with this structure, you could process it in one shot instead of line by line (for the example, I repeated several times this LineText that you provided and just incremented the AD value)..
>> buffer = fileread('brad.txt') ;
>> num = str2double(regexp(buffer,'\d+','match')) ;
>> num = reshape(num, 5, []).'
num =
0 43 0 0 0
0 44 0 0 0
0 45 0 0 0
0 46 0 0 0
and you could use more elaborate patterns if numbers were not integers, e.g. '[\d\.-]+' for matching positive and negative floating points as well.