read a text file, use strcmpi function
1 次查看(过去 30 天)
显示 更早的评论
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
3 个评论
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')
采纳的回答
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.
0 个评论
更多回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!