How to compare words in a line

조회 수: 1 (최근 30일)
Naga
Naga 2019년 8월 14일
편집: Adam Danz 2019년 8월 16일
Actually I have got a text file in which there are keywords in various lines(assume rows columns). If first keyword is matched with required word , then there is no need to check other keywords in same line (or next column of the line). In similar way , the keywords in second, third and so on lines should also verify just like in first line. So how to write loop for this task. One can assume these keywords are in a matrix . I think , for loop is useful but how exactly I have to proceed
  댓글 수: 2
Adam Danz
Adam Danz 2019년 8월 14일
편집: Adam Danz 2019년 8월 14일
We can easily make generic suggestions but without more detail or an example, the answer may not be specific enough to actually help.
After you read in your data and split the text by line, you can use ismember() or regexp() or contains() to determine if a list of key words exist within each line.
If you need more specific advice, please provide a sample of your text and a sample of the list of key words.
Do you need case sensitivity? Is your only goal to determine if any of the key words exist in each line (True/False output) ?
Naga
Naga 2019년 8월 15일
편집: Naga 2019년 8월 15일
A2Day A1Day euceh
A4HA becb
ABPt_Phase2 veh
The above one is text file with different words in it. These words should match with the keywords in dcm file. first word in the first line should be found in Dcm file ,If it found in it then it should stop searching in first line and have to move to search for word in next line.
For example A2 day in first line is not found , then it should search for A1Day . If A1Day is found, then it should stop searching for words in first line and go to second line. Again it should search for keywords in the second line in the same procedure as in first line.
The words are Case sensitive.
I have tried split function to split words in each line, but its showing error message that split function is not defined for char. My friend matlab software is working fine with "Split" keyword but my matlab version is 2012. That might be the issue
Can you tell me the other way to split the words and writing the logic?

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 8월 15일
편집: Adam Danz 2019년 8월 16일
Attached are 2 text files. "txt.txt" contains the text you shared in your comment under your question. "dcm.txt" is a vertical list of key words.
The 2 blocks of code below read in each file and separate the text by line. It returns a logical column vector "hasKey" where TRUE values indicate rows of txt that contain a key word.
For prior to r2016b
% Read in data
txt = fileread('txt.txt'); % Better to use a full path
key = fileread('dcm.txt'); % Better to use a full path
% separate text by line & remove newline char
txt = strtrim(regexp(txt,'\n','split').');
key = strtrim(regexp(key,'\n','split').');
% Loop through each line of txt and detect a key word
hasKey = false(size(txt));
for i = 1:numel(txt)
lineParse = strtrim(regexp(txt{i},' +','split'));
hasKey(i) = any(cellfun(@(x)any(strcmp(x,key)),lineParse));
% hasKey(i) = any(ismember(lineParse, key)); OLD VERSION, DON'T USE THIS LINE
end
For r2016b or later
Instead of strtrim(regexp()) you can use splitlines()).
% separate text by line
txt = splitlines(txt); % r2016b or later
key = splitlines(key); % r2016b or later
Result for both methods
hasKey =
3×1 logical array
1
0
1
% Row 1 and 3 contain a key word
  댓글 수: 5
per isakson
per isakson 2019년 8월 15일
편집: per isakson 2019년 8월 15일
Warning:
>> any(ismember( 'embedded words', 'bed' ))
ans =
logical
1
and
>> contains( 'embedded words', 'bed' )
ans =
logical
1
Is this what you want?
Adam Danz
Adam Danz 2019년 8월 16일
편집: Adam Danz 2019년 8월 16일
@per isakson, thanks for noticing that! I updated the answer.
@ nagasai thumati, you should definitely use the updated code. I just had to replace the last line within the for-loop.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by