2010-06-16 11 views
5

क्या आप मटलैब में एक टेक्स्ट फ़ाइल स्कैन करते समय खाली लाइनों को पहचानने का कोई तरीका है? मैं टेक्स्ट के बीच रिक्त रेखाओं के आधार पर फ़ाइलों को पार्स करना चाहता हूं। क्या यह संभव है?Matlab में खाली लाइनों को पहचानने का कोई तरीका है?

उत्तर

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

काम करता है मुझे लगता है कि वह "मैटलैब" कहा .. –

2

यहाँ एक संभावना है: अब बिना \ r ...

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

यह टिप्पणियों के साथ भी काम करता है: 'line = textscan (fid, '% s', 'commentStyle', '#')' – Wok

0

ठीक

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 
संबंधित मुद्दे