img.txt 라는 파일에 아래 내용이 저장되어 있다고 하자.


D:\DB\oxford\jpg\bark_img1.jpg

D:\DB\oxford\jpg\bark_img2.jpg

D:\DB\oxford\jpg\bark_img3.jpg

D:\DB\oxford\jpg\bark_img4.jpg

D:\DB\oxford\jpg\bark_img5.jpg



load(img.txt) 를 사용하면 에러 발생 (숫자만 읽어들일 수 있다고 한다)


fopen , fread 혹은 fscanf 해서 한라인씩 읽어들이려니 설정해줄 것도 많고 귀찮다. 


조금 잘못하면 에러 난다...


textscan 함수를 사용해보자.


fp = fopen('img.txt','r');

imFilelist = textscan(fp,'%s')


여기서 imFilelist는 cell 형식


cnt = length(imFilelist{1});  


이렇게하면 cnt에는 전체 string의 수가 저장된다.


for i = 1: cnt

%    imFile = cell2mat(imFilelist{1}(i)); % 뒤에 {} 괄호로 쓰면 cell2mat 안해도 되는 듯... 

    imFile = imFilelist{1}{i};

    ....

end


이렇게 하면 list에서 하나하나 파일을 꺼내올 수 있다.


* 참고로 엑셀파일을 불러올때는 xlsread 함수가 유용


* 참고로 여러 유형의 data가 섞여 있을 때는 아래와 같이 형식 지정해서 읽을 것

    fp = fopen(matched_ref_list{i},'r');

    imfilelist = textscan(fp,'%s\t%f');

 아래와 같은 식으로 access 가능

imfilelist{1}{j}

imfilelist{2}{j}




TEXTSCAN Read formatted data from text file or string.

    C = TEXTSCAN(FID,'FORMAT') reads data from an open text file identified

    by FID into cell array C. Use FOPEN to open the file and obtain FID. 

    The FORMAT is a string of conversion specifiers enclosed in single 

    quotation marks. The number of specifiers determines the number of 

    cells in the cell array C.  For more information, see "Format Options."

    

    C = TEXTSCAN(FID,'FORMAT',N) reads data from the file, using the FORMAT

    N times, where N is a positive integer. To read additional data from 

    the file after N cycles, call TEXTSCAN again using the original FID.

 

    C = TEXTSCAN(FID,'FORMAT','PARAM',VALUE) accepts one or more

    comma-separated parameter name/value pairs. For a list of parameters 

    and values, see "Parameter Options."

 

    C = TEXTSCAN(FID,'FORMAT',N,'PARAM',VALUE) reads data from the 

    file, using the FORMAT N times, and using settings specified by pairs

    of PARAM/VALUE arguments.

 

    C = TEXTSCAN(STR,...) reads data from string STR. You can use the 

    FORMAT, N, and PARAM/VALUE arguments described above with this syntax.

    However, for strings, repeated calls to TEXTSCAN restart the scan from 

    the beginning each time. (To restart a scan from the last position, 

    request a POSITION output.  See also Example 3.)

 

    [C, POSITION] = TEXTSCAN(...) returns the file or string position at 

    the end of the scan as the second output argument. For a file, this is 

    the value that FTELL(FID) would return after calling TEXTSCAN. For a 

    string, POSITION indicates how many characters TEXTSCAN read.  

Posted by 우주여행가
,