How to compare words in a line

2 visualizaciones (últimos 30 días)
Naga
Naga el 14 de Ag. de 2019
Editada: Adam Danz el 16 de Ag. de 2019
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 comentarios
Adam Danz
Adam Danz el 14 de Ag. de 2019
Editada: Adam Danz el 14 de Ag. de 2019
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 el 15 de Ag. de 2019
Editada: Naga el 15 de Ag. de 2019
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?

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 15 de Ag. de 2019
Editada: Adam Danz el 16 de Ag. de 2019
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 comentarios
per isakson
per isakson el 15 de Ag. de 2019
Editada: per isakson el 15 de Ag. de 2019
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 el 16 de Ag. de 2019
Editada: Adam Danz el 16 de Ag. de 2019
@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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by