matlab txt 文件操作

fid = fopen('result.txt', 'a') ;

fprintf( fid, '%-d\n', variable) ;

fclose( fid ) ;


%%%% matlab 打开文件后,逐行读取文件

fid = fopen('result.txt', 'r') ;

id = 1 ;
while 1
    tline=fgetl(fid);                        % read the context of a line 
    if ~ischar(tline),break;end
    tmp = [] ;
    for i =1:11                              % the first 11 is the lable of the data, 将前11个字符做成字符串
        tmp = [  tmp  tline(i)  ] ;
    end
    %%%%  tmp is a string, can be transformed into numbers.
    id_list{ id } = tmp ;
    id  =  id + 1 ;
end

fclose( fid ) ;


%%%%% 获取一行字符串中的各个数字,比如:0.97681 0.10723 0.64385 0.29556 1

%%%%% 需要做两件事:数字切分,读取各个数字

nums    =    str2num( tline ) ;  %将读取的一行tline, 转化为各个数值,这里nums是一个1*5的向量,

                                                   %  每个元素是一个double类型的数值,可以对数值直接操作。



%%%%matlab整个读取.txt文件,比如:整幅图像

A = load('G:\zz_myfile.txt') ;   % 直接load文件, 或使用fscanf读


%%%% matlab 逐行书写到文件

fid = fopen('result.txt', 'a') ;

for i = 1:length( id_list )
    tmp = [] ;
    tmp = ‘hello,world!’ ;
    fprintf( fid, '%s\n', tmp ) ;    %  write line by line 
end

fclose( fid ) ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值