2010-06-16 23 views

Antwort

2

Ja, das ist möglich. Ein MATLAB-Schnipsel würde in etwa so aussehen:

fid = fopen('reader.m'); 

newline = sprintf('\r\n'); 
line = fgets(fid); 
while ischar(line) 
    if strcmp(newline, line) 
     disp('Empty line'); 
    else 
     disp('Non-empty line'); 
    end 
    line = fgets(fid); 
end 
+3

Ich glaube, er sagte: "Matlab" .. –

2

Hier ist eine Möglichkeit:

fid = fopen('myfile.txt'); 
lines = textscan(fid, '%s', 'Delimiter', '\n'); 
fclose(fid); 
lines = lines{1}; 
% lines now contains a cell array of strings, 
% one per line in the file. 

% Find all the blank lines using cellfun: 
blank_lines = find(cellfun('isempty', lines)); 
+0

Es funktioniert auch mit Kommentaren: 'lines = textscan (fid, '% s', 'CommentStyle', '#')' – Wok

0

ohne \ r ... funktioniert jetzt

fid = fopen('reader.m'); 

newline = sprintf('\n'); 
line = fgets(fid); 
while ischar(line) 
    if strcmp(newline, line) 
     disp('Empty line'); 
    else 
     disp('Non-empty line'); 
    end 
    line = fgets(fid); 
end 
Verwandte Themen