필터 지우기
필터 지우기

Comparing characters in a matrix

조회 수: 1 (최근 30일)
Mini
Mini 2013년 2월 25일
I have a (nx1) matrix,M, that stores a list of characters and I need to iterate through a loop and keep comparing the i-th character in the matrix with a temp character.
But I'm not sure how to do this, i tried using M{i,1}== tmp and strcmp(M{i,1},tmp) but neither seem to work. Please help, thank you so much!
  댓글 수: 5
Mini
Mini 2013년 2월 25일
cmd = ['tesseract ',file,' tmp -psm 10']; %this is a command to run OCR and will save the character returned in a tmp.txt file
system(cmd);
%save temp results
f2 = fopen('tmp.txt','r');
tmp = fgets(f2); %this is the tmp character I want to compare against
if strcmp(M(i),tmp)
count = count+1;
end; if true
% code
end
Mini
Mini 2013년 2월 25일
oh dear I realised my mistake... its not working because the characters in M are stored with the apostrophes in the format: 'U','C','Y'... but in the variable tmp, the character are just stored as: U, C, Y... is there a way to solve this kind of problem?

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 25일
편집: Azzi Abdelmalek 2013년 2월 25일
strcmp(M(i),tmp)
  댓글 수: 9
Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 25일
편집: Azzi Abdelmalek 2013년 2월 25일
The problem is tmp is not equal to 'U' but to something like
'U ' % with spaces
Try this to remove space from tmp
strcmp(M(1),strtrim(tmp))
Mini
Mini 2013년 2월 25일
omg this works, thank you sooooooooo much!!! (: Hope you have a great week ahead!! (:

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

추가 답변 (1개)

Matt J
Matt J 2013년 2월 25일
편집: Matt J 2013년 2월 25일
count=sum([M{:}]==tmp)
  댓글 수: 4
Mini
Mini 2013년 2월 25일
oh thank you for the explanation... now i understand where you are coming from.. but this tmp changes value in every iteration, so in that case I will have to check it in every iteration right?
Matt J
Matt J 2013년 2월 25일
편집: Matt J 2013년 2월 25일
Not if there's a vectorized way of creating tmp. If M and tmp are string vectors of the same length, you can still do
count=sum(M==tmp)

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by