2015-05-14 61 views
0

我不熟悉Matlab。 我正在处理由字母数字和符号组成的数据。 我想将底线移到文件的最顶端。 我研究过使用fseek等的其他方法,但这些方法让我感到困惑,并且使用了超特定的文件特征。 有没有一个通用的方法来做到这一点?matlab:将文本文件的最后一行移动到顶部

谢谢你的时间。

回答

0

我认为这没有内置功能。但有一个简单的方法,我认为通过以下步骤:

  1. 阅读整个文件freadf
  2. 将矩阵中的最后一行移到第一行。
  3. 和写回的文件fwrite
0

对于字母数字文件的最简单易懂的方法是仔细阅读本文件,保存在一个字符串单元阵列行,并在适合你的顺序写。这些行应该在这里解决你的问题。

readfin = fopen('myfile.txt','r'); 
writefin = fopen('modifiedfile.txt', 'w'); 
lineno = 1; 

% Get all lines one by one and save them 
while(~feof(readfin)) 
    newline{lineno} = fgetl(readfin); 
    lineno = lineno + 1; 
end 

% Write first line 
fprintf(writefin, '%s', newline{end}); 

% Write the rest of the lines one by one 
for lineno=1:(length(newline)-1) 
    fprintf(writefin,'\n'); 
    fprintf(writefin, '%s', newline{lineno}); 
end 

fclose(readfin); 
fclose(writefin); 
相关问题