我有一个文本文件 (*.txt),其中某些行以空格开头,我想删除所有前导空格。 文本有将近 20k 行,其中某些随机行在开头有一个空格。因此,我很难阅读这些行。 我想删除前导空格,以便可以正确阅读。
最佳答案
尝试使用以下代码:
inputFileID=fopen('input.txt','r'); % Open input file for reading
outputFileID=fopen('output.txt','w'); % Open output file for writing
formatted_lines_in_cell_array = textscan(inputFileID,'%s','Delimiter','\n'); % Scan input file, and split to rows
formatted_lines=formatted_lines_in_cell_array {1,1}; % Extract the formatted lines array from the 1x1 cell array output of textscan
fprintf(outputFileID,'%s\r\n',formmated_lines{:}); % Write formatted lines to output file
fclose(inputFileID); % Close files
fclose(outputFileID);
关于matlab - 如何删除matlab中文本文件中的前导空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49974388/