MATLAB help always showing true to if condition

So, I wanted to write a program to classify text files based on their topic. The code below shows a very crude implementation of the same, but it isn't working properly. It is always showing a true value to the if condition, so I am getting Greetings four times and then Colloquial four times. What's up?
if true
File1 = fopen('Hello.xml','r');
File2 = fopen('Hello2.xml','r');
File3 = fopen('Colloquial1.xml','r');
File4 = fopen('Colloquial2.xml','r');
A = fscanf(File1, '%s');
B = fscanf(File2, '%s');
C = fscanf(File3, '%s');
D = fscanf(File4, '%s');
if ~( strcmp(A,'Hi') || strcmp(A,'Hello') || strcmp(A,'how'))
disp('Greetings.');
end
if ~( strcmp(B,'Hi') || strcmp(B,'Hello') || strcmp(B,'how'))
disp('Greetings');
end
if ~( strcmp(C,'Hi') || strcmp(C,'Hello') || strcmp(C,'how'))
disp('Greetings');
end
if ~( strcmp(D,'Hi') || strcmp(D,'Hello') || strcmp(D,'how'))
disp('Greetings');
end
if ~( strcmp(A,'lite'))
disp('Colloquial.');
end
if ~( strcmp(B, 'lite'))
disp('Colloquial');
end
if ~( strcmp(C, 'lite'))
disp('Colloquial');
end
if ~( strcmp(D, 'lite'))
disp('Colloquial');
end
end

3 件のコメント

Daniel Shub
Daniel Shub 2013 年 6 月 4 日
Have you thought about using REGEXP for this? It might be a little cleaner.
Samyukta Ramnath
Samyukta Ramnath 2013 年 6 月 4 日
But REGEXP is mainly for performing operations on the found substring, right? I just need to see if the word is there.
Daniel Shub
Daniel Shub 2013 年 6 月 4 日
No, REGEXPREP Does replacements, REGEXP just does the search.

サインインしてコメントする。

 採用された回答

Daniel Shub
Daniel Shub 2013 年 6 月 4 日

0 投票

The strcmp function requires an exact match. We can define a simple test function to see if a string is either exactly 'Hi' or exactly 'lite'
test = @(A)([strcmp(A,'Hi'), strcmp(A,'lite')])
Then
test('Hi')
1 0
test('lite')
0 1
test('Hilite')
0 0
In words: Hi is Hi, but it is not lite. lite is not Hi but is lite, and Hilite is neither Hi nor lite.
Are you potentially looking for STRFIND
test = @(A)([~isempty(strfind(A,'Hi')), ~isempty(strfind(A,'lite'))])
test('Hi')
1 0
test('lite')
0 1
test('Hilite')
1 1

5 件のコメント

Samyukta Ramnath
Samyukta Ramnath 2013 年 6 月 4 日
The file Hello has the content - 'Hello, Mr Anderson, how are you doing?' Hello1 has : 'Hello, my name is Samyukta.' Colloquial1 has : 'Arre no, take lite, ob you can’t do anything.' Colloquial2 has : 'Sam, gimme one donut, or else take lite!'
So now we know the exact contents, and strcmp should be able to find these correctly, right?
Matt Kindig
Matt Kindig 2013 年 6 月 4 日
Actually, if you look at Daniel's posting, you'll see that strcmp() won't do what you want to do, since the strings are not identically 'hi', 'hello', 'lite', etc. Use the strfind() method that he describes above.
Daniel Shub
Daniel Shub 2013 年 6 月 4 日
編集済み: Daniel Shub 2013 年 6 月 4 日
No, STRCMP requires an exact match of the entire string. You want strfind or regexp.
Samyukta Ramnath
Samyukta Ramnath 2013 年 6 月 5 日
Thank you! That works. :)
Daniel Shub
Daniel Shub 2013 年 6 月 6 日
Please consider accepting an answer if your problem is solved. Also, please consider voting for other answers that were helpful.

サインインしてコメントする。

その他の回答 (2 件)

Ken Atwell
Ken Atwell 2013 年 6 月 3 日

0 投票

It is not clear what the contents of the files are and what the expected ("correct") output ought to be. Be aware that strcmp returns different results than a C programmer might expect:
strcmp might be returning 1 where you are expecting 0.
If that is not the problem, you will need to provide more context -- namely, the first line of the four files you are parsing.

3 件のコメント

Samyukta Ramnath
Samyukta Ramnath 2013 年 6 月 3 日
The Files which say Hello have Hi and Hello in them, and the files which have Colloquial in their name have the word 'lite' in them. So, it should not be returning a true for everything. And I realized that one error was that the strcmp returns a value different from that in C.
Ken Atwell
Ken Atwell 2013 年 6 月 3 日
Take out the not operators (the difference between MATLAB and C) and it seems to work okay for me:
File1 = fopen('Hello.xml','r');
File2 = fopen('Hello2.xml','r');
File3 = fopen('Colloquial1.xml','r');
File4 = fopen('Colloquial2.xml','r');
A = fscanf(File1, '%s');
B = fscanf(File2, '%s');
C = fscanf(File3, '%s');
D = fscanf(File4, '%s');
if strcmp(A,'Hi') || strcmp(A,'Hello') || strcmp(A,'how')
disp('Greetings A');
end
if strcmp(B,'Hi') || strcmp(B,'Hello') || strcmp(B,'how')
disp('Greetings B');
end
if strcmp(C,'Hi') || strcmp(C,'Hello') || strcmp(C,'how')
disp('Greetings C');
end
if strcmp(D,'Hi') || strcmp(D,'Hello') || strcmp(D,'how')
disp('Greetings D');
end
if strcmp(A,'lite')
disp('Colloquial A');
end
if strcmp(B, 'lite')
disp('Colloquial B');
end
if strcmp(C, 'lite')
disp('Colloquial C');
end
if strcmp(D, 'lite')
disp('Colloquial D');
end
if true
% code
end
Output is:
Greetings A
Greetings B
Colloquial C
Colloquial D
Samyukta Ramnath
Samyukta Ramnath 2013 年 6 月 4 日
Oh My. I copied and pasted this exact code into my MATLAB script and ran it, and nothing happened.

サインインしてコメントする。

Iain
Iain 2013 年 6 月 4 日

0 投票

Do A, B, C, and D actually have those strings in them, EXACTLY like what you're comparing them to?
I suspect they have a different case. Try strcmpi instead of strcmp.
What do you get when you get matlab to print out A, B, C & D?

1 件のコメント

Daniel Shub
Daniel Shub 2013 年 6 月 4 日
For STRCMP it is not about having the strings IN them, it is about BEING them.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by