2012-12-11 76 views
1

我試圖尋找招呼一個文本文件,裏面MATLAB用下面的代碼:使用validatestring的正確方法?

fid = fopen(filename, 'r'); 
if (fid == -1) 
    error('cnt open'); 
end 

i = 0; 
while (i == 0) 

    str = 'hello'; 
    validStrings = fgets(fid); 
    validStr = validatestring(str,validStrings); 
    disp(validStr) 
    if (line == -1) 
     i = 1; 
    else 
    fprintf(1, '%s', line); 
    end 

end 

fclose(fid); 

我不能在這種情況下使用validStrings?我得到錯誤

??? Error using ==> validatestring 
Valid strings must be a cell array of strings. 

回答

0

函數需要字符串單元:

A = cell(100,1); // use a bigger number if you have a large file 
while (i == 0) 
    str = 'hello'; 
    validStrings = fgets(fid); 
    j = 1; 
    [A{j} remain] = strtok(validStrings, ' '); //or other delimiter than spaces 
    while(size(remain,2) ~= 0) 
     [A{j} remain] = strtok(str, ' '); 
     j = j+1; 
    end 
    validStr = validatestring(str,A); 
    disp(validStr) 
    if (line == -1) 
     i = 1; 
    else 
     fprintf(1, '%s', line); 
    end 
end