MATLAB: How to read text file which has blanks in some columns

reading from text file

I have a text file in the following format :
Algorithm SNR Q
A 20 30
25 32
30 35
B 20 32
25 34
30 36
I want to read this data of SNR and Q for 2 different algorithms so that i can plot SNR vs Q graph.
But the problem is that when I read this text file using Matlab functions (example textscan) , it doesn't read it correctly due to the empty spaces present in this text file in the column 'Algorithm'. For example: I tried to open this text file and read it and then display its contents :
fid = fopen('Test.txt');
C = textscan(fid,'%s %d %f');
fclose(fid);
celldisp(C);
The output of the above code is :
C{1}{1} = Algorithm
C{2} = []
C{3} = []
If I remove the first column named 'Algorithm' from the text file and then read that file, then it works fine. But I am unable to read it correctly when that String column is also present. Can anyone please help me to know how can I read such data correctly from a text file. Thanks for your help in advance.

Best Answer

There are quite a few approaches. Here one example:
fh = fopen('Swati.txt', 'r') ;
fgetl(fh) ; % Skip header.
algs = {} ;
data = {} ;
while ~feof(fh)
line = fgetl(fh) ;
if all(line(1:12) == ' ') % No algo. name.
content = textscan(line, '%f %f') ; % Read only SNR and Q.
data{end} = [data{end}; [content{:}]] ; % Append to current data.
else
content = textscan(line, '%s %f %f') ; % Read name, SNR, and Q.
algs = [algs; content(1)] ; % Append to names.
data = [data; {[content{2:3}]}] ; % Create new data entry.
end
end
fclose(fh) ;
Running this leads to:
>> algs
algs =
{1x1 cell}
{1x1 cell}
>> algs{1}
ans =
'A'
>> data
data =
[3x2 double]
[3x2 double]
>> data{1}
ans =
20 30
25 32
30 35
Please note that it is not the best possible approach/practice, but it is simple enough.