我不熟悉Matlab。 我正在处理由字母数字和符号组成的数据。 我想将底线移到文件的最顶端。 我研究过使用fseek等的其他方法,但这些方法让我感到困惑,并且使用了超特定的文件特征。 有没有一个通用的方法来做到这一点?matlab:将文本文件的最后一行移动到顶部
谢谢你的时间。
我不熟悉Matlab。 我正在处理由字母数字和符号组成的数据。 我想将底线移到文件的最顶端。 我研究过使用fseek等的其他方法,但这些方法让我感到困惑,并且使用了超特定的文件特征。 有没有一个通用的方法来做到这一点?matlab:将文本文件的最后一行移动到顶部
谢谢你的时间。
对于字母数字文件的最简单易懂的方法是仔细阅读本文件,保存在一个字符串单元阵列行,并在适合你的顺序写。这些行应该在这里解决你的问题。
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);
这样如何从这样的回答: MATLAB: How do you insert a line of text at the beginning of a file?
fid = fopen(filepath);
while 1
line = fgetl(fid);
if ~ischar(line)
break;
end
pline = line;
end
fclose(fid);
dlmwrite(filepath,[pline 13 10 fileread(filepath)],'delimiter','');