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
2013년 6월 4일
Have you thought about using REGEXP for this? It might be a little cleaner.
Samyukta Ramnath
2013년 6월 4일
Daniel Shub
2013년 6월 4일
No, REGEXPREP Does replacements, REGEXP just does the search.
채택된 답변
추가 답변 (2개)
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
2013년 6월 3일
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
2013년 6월 4일
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
2013년 6월 4일
For STRCMP it is not about having the strings IN them, it is about BEING them.
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!