[問題] 可以和檔案中的字串比較嗎

看板MATLAB作者 (shin)時間14年前 (2012/07/09 14:18), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/1
如題,輸入一字串後,想和檔案內的字串比較用以偵錯 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

07/09 15:09, , 1F
用textread、fgets、fscanf之類的函數把檔案存進變數才能比
07/09 15:09, 1F
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)
文章代碼(AID): #1F-dUOrA (MATLAB)
文章代碼(AID): #1F-dUOrA (MATLAB)