필터 지우기
필터 지우기

read a text file, use strcmpi function

조회 수: 2 (최근 30일)
Mohammed Hamed
Mohammed Hamed 2019년 1월 1일
댓글: Mohammed Hamed 2019년 1월 8일
i would like the matlab to read a text file which are like :
i used strcmpi fuction but it didn't work.
so i wwould like some help so i can read junctions, reservoirs, pipes. each one alone
toggle = true;
while toggle== true
aline = fgetl(fid);
% test whether the line begins with "Start", and flip toggle
if strcmpi(aline(1:5),'[junc')
toggle = false;
end
end
Capture.PNG
  댓글 수: 3
Rik
Rik 2019년 1월 1일
@vik, you can move this comment to the answer section. I would only add that the code Mohammed posted probably failed on an empty line, due to a length check missing.
@Mohammed, you can either use the code vik suggested, or use the contains function instead of comparing the first few chars. You could also keep your current code and add in a test for size:
if numel(aline>=5) && strcmpi(aline(1:5),'[junc')
Mohammed Hamed
Mohammed Hamed 2019년 1월 8일
The original data file.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2019년 1월 2일
toggle = true;
while toggle % not required, because it is a logical already: == true
aline = fgetl(fid);
toggle = ~strncmpi(aline, '[junc', 5)
end
strncmpi works also with lines, which are shorter than 5 characters. Instead of the if clause, you can set the value of toggle directly.

추가 답변 (1개)

vik
vik 2019년 1월 2일
편집: vik 2019년 1월 3일
A quick and dirty code without preallocation to read the attached .txt-file would be like this:
filename='example_437840.txt';
fileID = fopen(filename,'r','ieee-le','UTF-8'); % open file
data_rows = textscan(fileID,'%s','Delimiter','\n'); % read whole file
fclose(fileID); % close file
n_rows = size(data_rows{1,1},1); % Determine number of rows
% Init some counters
idx_jun = uint32(0);
idx_res = uint32(0);
idx_pip = uint32(0);
for nf_row = 3:n_rows % Start at Line 3
if ~isempty(data_rows{1,1}{nf_row,1}) % Check if not empty
row_current = ... % Get current row and split with Tab as delimiter:
textscan(data_rows{1,1}{nf_row,1},'%s','Delimiter','\t');
% Determine what to do if line starts with a new heading
if strcmp(row_current{1,1}{1,1}(1,1),'[') % If line starts with [
% Get the String between those two bracktes:
m = row_current{1,1}{1,1}(2:end-1); % and set the switch-thing
elseif ~strcmp(row_current{1,1}{1,1}(1,1),';') % also skip ;-Lines
% In all other cases, read lines depending on what to do:
switch(m)
case 'JUNCTIONS'
idx_jun = idx_jun + 1; % Increase counter
% Get Data from the current row split at tabs:
jun_id(idx_jun) = str2double(row_current{1,1}{1,1});
jun_elev(idx_jun) = str2double(row_current{1,1}{2,1});
jun_dem(idx_jun) = str2double(row_current{1,1}{3,1});
case 'PIPES'
idx_pip = idx_pip + 1;
% Get Data from the current row split at tabs:
pip_id(idx_pip) = str2double(row_current{1,1}{1,1});
pip_node1(idx_pip) = str2double(row_current{1,1}{2,1});
pip_node2(idx_pip) = str2double(row_current{1,1}{3,1});
pip_length(idx_pip) = str2double(row_current{1,1}{4,1});
case 'RESERVOIRS'
idx_res = idx_res + 1;
% Same as above and more cases maybe
end % switch(m)
end % if strcmp(row_current{1,1}{1,1}(1,1),'[')
end %if ~isempty(data_rows{1,1}{nf_row,1})
end %for nf_row = 3:n_rows
It worked so far but may need some more optimization in case the file differs. Maybe there is a more nice way to extract the string between those two square brackets.
  댓글 수: 1
Mohammed Hamed
Mohammed Hamed 2019년 1월 8일
Thank you indead for your support, but the code you provide only works in a specific file.
The number of pipes, junction, reservoirs are changed in each file

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by