From: John Zoidberg on
Hi,

I'm trying to parse a file like this:
TYPE
{
DATA1 **comments
DATA2 **comments
}

My problem is that I can't get rid of the newline after the first "{".

At the end is a current version of my parser and a sample file.

Instead of:
========
ans =





ans =

1


ans =

2


ans =

3

========

I want:
========
ans =

1


ans =

2


ans =

3
========

How can I achieve this?
(Ideally, spaces or anything after "{" should also be excluded.)

parser.m
===================
function parser(filename)
if exist('filename','var')==0
disp('filename not given');
filename = uigetfile({'*.txt'},'Select a file');
end

fulltext = fileread(filename);

pattern_stripcomments = '\*\*.*$';
cleantext = regexprep(fulltext, pattern_stripcomments, '\n', 'lineanchors', 'dotexceptnewline', 'warnings');

pattern_cleantext = '^(?<type>\w+).*?\{(?<data>[^\{\}]*?)\}';
[tokens_cleantext match_cleantext names_cleantext] = regexp(cleantext, pattern_cleantext, 'tokens', 'match', 'names', 'lineanchors', 'warnings');
% tokens_cleantext{:}
% match_cleantext{:}
% length(match_cleantext)
% names_cleantext(:,1)
% names_cleantext(1,:).type

% return

% for i=1:length(names_cleantext)
for i=1:1

type = names_cleantext(:,i).type;
data = names_cleantext(:,i).data;
disp(['type=',type]);
disp(['data=',data]);
pat2 = regexptranslate('escape', data)
% return

pattern_data = '^([^\n]+)$';
[tokens_data match_data names_data] = regexp(data, pattern_data, 'tokens', 'match', 'names', 'dotexceptnewline', 'lineanchors', 'warnings');
% tokens_data{:}
match_data{:}

end

end

===================

example.txt
===================
** sample file

type1
{
1 **x
2 **y
3 **z
}

** comments

type2
{
1 **x
2 **y
3 **z
}

===================
From: John Zoidberg on
Problem solved using strread(data,'%s','delimiter',['\r']);

Working parser:
==========
function parser(filename)

% ask for input file if not given
if exist('filename','var')==0
disp('filename not given');
filename = uigetfile({'*.txt'},'Select a file');
end

% read the whole file as one string
fulltext = fileread(filename);

% remove comments
pattern_stripcomments = '\*\*.*$';
cleantext = regexprep(fulltext, pattern_stripcomments, '\n', 'lineanchors', 'dotexceptnewline', 'warnings');

% extract blocks
pattern_blocks = '^(?<type>\w+).*?\{(?<data>[^\{\}]*?)\}';
[tokens_blocks match_blocks names_blocks] = regexp(cleantext, pattern_blocks, 'tokens', 'match', 'names', 'lineanchors', 'warnings');

% process blocks
for i=1:length(names_blocks)

type = names_blocks(:,i).type;
data = names_blocks(:,i).data;
disp(['===>type=',type]);

% remove empty lines
lines = strread(data,'%s','delimiter',['\r']);
for L=1:length(lines)
if length(lines{L}) ~= 0
disp(lines{L});
end
end
end

end