How can I write strings and matrices to a .txt file in MATLAB?

后端 未结 4 1370
一个人的身影
一个人的身影 2021-02-06 03:16

I need to write data to a .txt file in MATLAB. I know how to write strings (fprintf) or matrices (dlmwrite), but I need something that can do

4条回答
  •  余生分开走
    2021-02-06 04:13

    You don't need the empty matrix call. Try this code:

    str = 'This is the matrix: ' ;
    mat1 = [23 46 ; 56 67] ;
    fName = 'output.txt';
    fid = fopen('output.txt','w');
    if fid>=0
        fprintf(fid, '%s\n', str)
        fclose(fid)
    end
    dlmwrite(fName, mat1, '-append', 'newline', 'pc', 'delimiter','\t');
    

提交回复
热议问题