作者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)