MATLAB程序去除文件中的空白行

 有时间我们会遇到如下的文件:

figure;

I1=imread('1.jpg');


imshow(I1);


hold on;
Loc1=[y,y];


plot(Loc1,'r*');

% plot(Y) creates a 2-D line plot of the data in Y versus the indey of each value.

% If Y is a matriy, then the plot function plots the columns of Y versus their row number.


% The y-ayis scale ranges from 1 to the number of rows in Y

 这个文件含有很多空行,这实在是一个不太好看的文件。你希望看到的文件是不是这样的:

figure;
I1=imread('1.jpg');
imshow(I1);
hold on;
Loc1=[y,y];
plot(Loc1,'r*');
% plot(Y) creates a 2-D line plot of the data in Y versus the indey of each value.
% If Y is a matriy, then the plot function plots the columns of Y versus their row number.
% The y-ayis scale ranges from 1 to the number of rows in Y

      如果我想去除这些空行干怎么办啦,手动删除?文件短小,手动来也快,大了怎么搞? 这里就用Matlab程序来搞定这个事情,其他程序语言应该也行只是好不好实现罢了,估计Python干这个比较简洁。不多说直接上解决的程序:

function RemoveFileEmptyRow(ReadFilePath,WriteFilePath)
%% 2017/06/22 by DQ 
% 此算法为移除文本中空行
FidRead=fopen(ReadFilePath,'rb','ieee-le','GBK');
FidWrite=fopen(WriteFilePath,'wb','ieee-le','GBK');
while ~feof(FidRead)
    FileRowStr = fgetl(FidRead);
    if ~isempty(FileRowStr )
    fprintf(FidWrite,'%s\n',FileRowStr);
    end
end
fclose(FidRead);
fclose(FidWrite);
end

      好了把含有空行的文件命名为f1.txt,整理后的文件命名为f2.txt,然后运行一下上面这个程序,你就会得到想要的结果了。

clc;
clear;
close all;
ReadFilePath='f1.txt';
WriteFilePath='f2.txt';
RemoveFileEmptyRow(ReadFilePath,WriteFilePath)

     上面得到的结果保存在f2.txt,去这个文件看看是不是下面的内容:

figure;
I1=imread('1.jpg');
imshow(I1);
hold on;
Loc1=[y,y];
plot(Loc1,'r*');
% plot(Y) creates a 2-D line plot of the data in Y versus the indey of each value.
% If Y is a matriy, then the plot function plots the columns of Y versus their row number.
% The y-ayis scale ranges from 1 to the number of rows in Y
    当很多文件出现这种状况,这种处理方式还是很便捷的把



  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值