MATLAB Answers

Compare and find multiple matching strings

조회 수: 21(최근 30일)
Xiaohan Du
Xiaohan Du 22 Mar 2018
Commented: Xiaohan Du 22 Mar 2018
Hi all,
I have a text file (.inp) from Abaqus, where I need to find the line number of some strings. I read the file using 'fopen' and 'textscan' into MATLAB, results in string cells. The file looks like this:
rawInpStr
rawInpStr =
1×1 cell array
{1557×1 cell}
rawInpStr{:} =
{'*Heading' }
{'** Job name: l9h2SingleInc Model name: Model-1' }
{'** Generated by: Abaqus/CAE 6.12-4' }
{'*Preprint, echo=NO, model=NO, history=NO, contact=NO' }
{'** PARTS' }
{'*Part, name=beam' }
{'*End Part' }
{'** ASSEMBLY' }
{'*Assembly, name=Assembly' }
{'*Instance, name=beam-1, part=beam' }
{'*Node' }
......
For example, if I need to find 3 lines matching '** Job name', '** PARTS', '*End Part', I write:
for iStr = 1:length(rawInpStr{1})
strComp = strtrim(rawInpStr{1}{iStr});
if length(strComp) > 10
if strcmp(strComp(1:11), '** Job name') == 1
loc1 = iStr;
end
end
if length(strComp) > 7
if strcmp(strComp(1:8), '** PARTS') == 1
loc2 = iStr;
end
end
if length(strComp) > 8
if strcmp(strComp(1:9), '*End Part') == 1
loc3 = iStr;
end
end
end
So for N strings I'd want to find, I need N for loops. Is there a more elegant way to do this?

  댓글 수: 0

로그인 to comment.

채택된 답변

Guillaume
Guillaume 22 Mar 2018
Guillaume 님이 편집함. 22 Mar 2018
The loop was never needed in the first place:
loc1 = find(strncmp(rawInpStr{1}, '** Job name', 11))
For several search strings, you can loop over your search strings (or use cellfun):
searchstrings = {'** Job name', '** PARTS', '*End part'};
locs = cell(size(searchstrings); %using a cell array in case the search string is found several time through the file
for searchidx = 1:numel(searchstrings)
locs{searchidx} = find(strncmp(rawInpStr{1}, searchstrings{searchidx}, numel(searchstrings{searchidx})));
end
Note that if your ultimate goal is to find a pattern over several lines, then I would take a completely different approach.
edit: As you can gather from my answer, for what you're doing strncmp is a lot more useful than strcmp.
editedit: Used the proper variables.

  댓글 수: 3

Xiaohan Du
Xiaohan Du 22 Mar 2018
when you say 'The loop was never needed in the first place', I don't understand, how can you extract variable 'strComp' without a for loop? Should the complete code be the following?
for iStr = 1:length(rawInpStr{1})
strComp = rawInpStr{1}(iStr);
loc1 = find(strncmp(strComp, '** Job name', 11))
end
Guillaume
Guillaume 22 Mar 2018
Yes, the loop was never needed in the first place, strncmp (and strcmp) can work directly on the whole cell array. However, I read your code too quickly and didn't realise that strComp was only one element of the cell array. The proper example should have been:
loc1 = find(strncmp(rawInpStr{1}, '** Job name', 11));
Same for the full solution, use rawInpStr{1} directly. I've fixed my answer.
Xiaohan Du
Xiaohan Du 22 Mar 2018
okay I got it, this is much better! Many thanks!

로그인 to comment.

More Answers (1)

thitch
thitch 22 Mar 2018
The following may help to get you started:
rawInpStr = {'cat','dog','whale'};
mySearchTerms = {'cat','dog','whale','camel'};
foundOnLine = zeros(size(rawInpStr,2),size(mySearchTerms,2));
for lv1 = 1:size(mySearchTerms,2)
foundOnLine(:,lv1) = cellfun(@(x) strcmp(x,mySearchTerms{lv1}),rawInpStr);
end

  댓글 수: 1

Xiaohan Du
Xiaohan Du 22 Mar 2018
Thanks!

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by