read a text file, use strcmpi function

1 回表示 (過去 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 Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by