input.txt를 output.txt 로 통채로 카피하고 싶을때  

(파일의 종류는 무엇이 되든 상관없음. 사진이든 뭐든 상관없음))


copyfile('input.txt','output.txt');


주의, copyfile의 두 인자는 파일의 이름 (m파일과 같은 폴더에 있는 경우) 

       혹은 주소를 넣어주어야 한다.




COPYFILE   Copy file or directory.

    [SUCCESS,MESSAGE,MESSAGEID] = COPYFILE(SOURCE,DESTINATION,MODE) copies

    the file or directory SOURCE to the new file or directory DESTINATION.

    Both SOURCE and DESTINATION may be either an absolute pathname or a

    pathname relative to the current directory. When the MODE is set to

    'f', COPYFILE copies SOURCE to DESTINATION, even when DESTINATION is

    read-only. The DESTINATION's writable attribute state is preserved. 

 

    [SUCCESS,MESSAGE,MESSAGEID] = COPYFILE(SOURCE) attempts to copy SOURCE

    to the current directory.

 

    [SUCCESS,MESSAGE,MESSAGEID] = COPYFILE(SOURCE, DESTINATION) attempts to

    copy SOURCE to DESTINATION. If SOURCE constitutes a directory or

    multiple files and DESTINATION does not exist, COPYFILE attempts to

    create DESTINATION as a directory and copy SOURCE to DESTINATION. If

    SOURCE constitutes a directory or multiple files and DESTINATION exists

    as a directory, COPYFILE attempts to copy SOURCE to DESTINATION. If

    SOURCE constitutes a directory or multiple files and none of the above

    cases on DESTINATION applies, COPYFILE fails.

 

    [SUCCESS,MESSAGE,MESSAGEID] = COPYFILE(SOURCE,DESTINATION,'f') attempts

    to copy SOURCE to DESTINATION, as above, even if DESTINATION is

    read-only. The status of the writable attribute of DESTINATION will be

    preserved.

 

    INPUT PARAMETERS:

        SOURCE:      1 x n string, defining the source file or directory.

        DESTINATION: 1 x n string, defining destination file or directory.

                     The default is the current directory. 

        MODE:        character scalar defining copy mode.

                     'f' : force SOURCE to be written to DESTINATION. If

                     omitted, COPYFILE respects the current writable status

                     of DESTINATION. 

 

    RETURN PARAMETERS:

        SUCCESS:     logical scalar, defining the outcome of COPYFILE.

                     1 : COPYFILE executed successfully. 0 : an error

                     occurred.

        MESSAGE:     string, defining the error or warning message.

                     empty string : COPYFILE executed successfully. message

                     : an error or warning message, as applicable.

        MESSAGEID:   string, defining the error or warning identifier.

                     empty string : COPYFILE executed successfully. message

                     id: the MATLAB error or warning message identifier

                     (see ERROR, MException, WARNING, LASTWARN).

 

    NOTE 1: Except where otherwise stated, the rules of

            the underlying system on the preservation of attributes are

            followed when copying files and directories.

    NOTE 2: The * wildcard is supported in the filename or extension.

 

    See also cd, delete, dir, fileattrib, mkdir, movefile, rmdir.


    Reference page in Help browser

       doc copyfile

Posted by 우주여행가
,

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 우주여행가
,

newstring = strrep(orgstring, word1,word2)


strrep 함수는 orgstring에서 word1을 찾아서 word2로 바꾸어 newstring에 저장한다.


예제)


str1 = 'lonesome';

str2 = strrep(str1,'lone', 'awe');


결과 str2 = 'awesome'




 STRREP Replace string with another.

    MODIFIEDSTR = STRREP(ORIGSTR,OLDSUBSTR,NEWSUBSTR) replaces all 

    occurrences of the string OLDSUBSTR within string ORIGSTR with the

    string NEWSUBSTR.

 

    Notes:

 

    * STRREP accepts input combinations of single strings, strings in 

      scalar cells, and same-sized cell arrays of strings. If any inputs 

      are cell arrays, STRREP returns a cell array. 

 

    * STRREP does not find empty strings for replacement. That is, when

      ORIGSTR and OLDSUBSTR both contain the empty string (''), STRREP does

      not replace '' with the contents of NEWSUBSTR.

 

    Examples:

 

    % Example 1: Replace text in a character array.

 

        claim = 'This is a good example';

        new_claim = strrep(claim, 'good', 'great')

 

        new_claim = 

        This is a great example.

 

    % Example 2: Replace text in a cell array.

 

        c_files = {'c:\cookies.m'; ...

                   'c:\candy.m';   ...

                   'c:\calories.m'};

        d_files = strrep(c_files, 'c:', 'd:')

 

        d_files = 

            'd:\cookies.m'

            'd:\candy.m'

            'd:\calories.m'

 

    % Example 3: Replace text in a cell array with values in a second cell

    % array.

 

        missing_info = {'Start: __'; ...

                        'End: __'};

        dates = {'01/01/2001'; ...

                '12/12/2002'};

        complete = strrep(missing_info, '__', dates)

 

        complete = 

            'Start: 01/01/2001'

            'End: 12/12/2002'

     

    See also strfind, regexprep.


    Reference page in Help browser

       doc strrep

Posted by 우주여행가
,