1. 程式人生 > >matlab查詢指定資料夾下檔案(附漢字和標點符號讀取方法)

matlab查詢指定資料夾下檔案(附漢字和標點符號讀取方法)

fidrrt = fopen('F:\lyn.txt');
while 1
    tline = fgetl(fidrrt);
    if ~ischar(tline),   break,   end
    tline =  native2unicode(tline);
    disp(tline)
end
fclose(fidrrt);
經測試正確
輸出:
你好 你好 的 ; 你們  %第一行
, 好  %第二行

迴圈讀取一個資料夾中制定字尾的檔案方法:
clc;
clear all;
載入資料夾
pathname = uigetdir(cd, '請選擇資料夾');
if pathname == 0
    msgbox('您沒有正確選擇資料夾');
    return;
end

%搜尋指定格式檔案
pathname = strcat(pathname, '\');
[read_file_list] = batch_read_file(pathname, '*.txt');

len = length(read_file_list);
%% 迴圈讀取每個檔案
for i = 1:len
    filepath = read_file_list(i);
    fidrrt = fopen('f:\1business.seg.cln.txt');
    
    while 1
        tline = fgetl(fidrrt);
        if ~ischar(tline),   break,   end
        tline =  native2unicode(tline);%% 中文顯示正常,檔案可以正常讀完。
        disp(tline)
    end
    fclose(fidrrt);
end
 read_file_list 函式如下:
function [read_file_list] = batch_read_file(direction, file_type)
%
file_read = dir(fullfile(direction, file_type)); %獲取要讀取的檔案列表
if strcmp(file_type, '*.txt'); %讀取txt檔案
    for i = 1:length(file_read)
        read_file_list{i} = strcat(direction, file_read(i).name); %獲取檔名的列表
    end
end
 
end
轉載請註明出處: http://blog.csdn.net/ningyaliuhebei/article/details/46342087