read a text file, use strcmpi function

2 Ansichten (letzte 30 Tage)
Mohammed Hamed
Mohammed Hamed am 1 Jan. 2019
Kommentiert: Mohammed Hamed am 8 Jan. 2019
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 Kommentare
Rik
Rik am 1 Jan. 2019
@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 am 8 Jan. 2019
The original data file.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 2 Jan. 2019
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.

Weitere Antworten (1)

vik
vik am 2 Jan. 2019
Bearbeitet: vik am 3 Jan. 2019
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 Kommentar
Mohammed Hamed
Mohammed Hamed am 8 Jan. 2019
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by