2010-06-16 74 views

回答

2

是的,这是可能的。一个MATLAB代码片段看起来是这样的:

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

我想,他说: “Matlab的” .. – 2010-06-16 22:17:15

2

这里有一个可能性:

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

它也适用于注释:'lines = textscan(fid,'%s','CommentStyle','#')' – Wok 2012-09-13 11:46:34

0

没有\ r ...现在工作得很好

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 
相关问题