read a text file, use strcmpi function
4 views (last 30 days)
Show older comments
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/199994/Capture.png)
3 Comments
Rik
on 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')
Accepted Answer
Jan
on 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.
0 Comments
More Answers (1)
vik
on 2 Jan 2019
Edited: vik
on 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.
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!