作者xj04a83 (shin)
看板MATLAB
標題[問題] 可以和檔案中的字串比較嗎
時間Mon Jul 9 14:17:57 2012
如題,輸入一字串後,想和檔案內的字串比較用以偵錯
test.txt
0.203600
0.203650
0.203700
...
程式碼:
file = fopen('test.txt','r');
string = num2str(0.203600);
if strcmp(string,file) == 0
warning('wrong');
end
但是卻發現無論如何strcmp回傳的都是0...
是因為strcmp不能和檔案比較嗎?
若是,那該用什麼來偵錯??
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.99.210
1F:→ DKer:用textread、fgets、fscanf之類的函數把檔案存進變數才能比 07/09 15:09
file = fopen('test.txt','r');
string = num2str(0.203600);
while 1
judge = strtrim(fgets(file))
strcmp(string,judge)
if strcmp(string,judge) == 1
break
elseif ~ischar(judge) && strcmp(string,judge) == 0
warning('wrong');
break
end
end
如果string設成字母就可以
但若是數字就無法work...
※ 編輯: xj04a83 來自: 140.114.99.210 (07/10 11:00)