how to read a matrix from a text file in matlab

前端 未结 4 1657
半阙折子戏
半阙折子戏 2021-01-15 08:51

I have a text file which has 500 columns and 500 rows, of numerical(integer) values . Every element in the row is separated by a tab. I want to rea

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 09:00

    % Pre-allocate matrix
    Nrow=500; Ncol=500;
    a = zeros(Nrow,Ncol);
    % Read file
    fid = fopen('yourfile.txt','r');
    for i:1:Nrow
       a(i,:) = cell2mat(textscan(fid,repmat('%d ',Ncol));
    end
    fclose(fid); 
    % Trasnspose matrix
    a_trans = a.';
    

提交回复
热议问题