读取mat文件中特定数据保存到txt中

最近在做faster-rcnn的内容,数据集标签给的格式是mat版本,而且标签内容有些是不需要的,想着提取需要的项目然后存在txt中,自己写了个matlab文件:

file_path = './';
mat_path_list = dir(strcat(file_path,'*.mat'));
mat_num = length(mat_path_list);
fprintf('%d\n',mat_num);
%fid = fopen('test.txt', 'wt');
mat_tmpfile_path = '../mat_tmp/';  %mat中间文件放置处
txt_tmpfile_path = '../txt_tmp/';  %最后txt文件放置处

%  for i=1:mat_num
for k = 1 : mat_num
    mat_name = mat_path_list(k).name;
    fprintf('%s\n',mat_name);
    
    %%%%生成无后缀的文件名,为了方便最后的txt文件命名
    jj = find('.' == mat_name);
    name_tmp = mat_name(1:jj-1);
    fprintf('%s\n',name_tmp);
    name_tmp1 = strcat(mat_tmpfile_path,strcat(name_tmp,'-1'));
    name_tmp2 = strcat(mat_tmpfile_path,strcat(name_tmp,'-2'));
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    fprintf('%s\n',mat_name);
    load(mat_name);
    save(name_tmp1,'-struct','record');
    load(name_tmp1,'objects');
    txtfile = strcat(name_tmp,'.txt');
    txtfile = strcat(txt_tmpfile_path,txtfile);
    if exist(txtfile,'file')
        delete(txtfile);
    end
    fid = fopen(txtfile, 'wt');
    c = length(objects);
    for i=1 : c
        ss = objects(i).class;
        fprintf('%s\n',ss);
        if strcmp(ss,'bed')
            class_index = 0;
        elseif strcmp(ss,'bookshelf')
            class_index = 1;
        elseif strcmp(ss,'cabinet')
            class_index = 2;
        elseif strcmp(ss,'chair')
            class_index = 3;
        elseif strcmp(ss,'sofa')
            class_index = 4;
        elseif strcmp(ss,'diningtable')
            class_index = 5;
        else
            continue;
        end
        fprintf(fid,'%d ',class_index);
        for j=1 : 4
            fprintf(fid,'%6.4f ',objects(i).bbox(j));
        end
        fprintf(fid,'%6.4f ',objects(i).viewpoint.azimuth_coarse);
        fprintf(fid,'%6.4f ',objects(i).viewpoint.elevation_coarse);
        fprintf(fid,'%6.4f',objects(i).viewpoint.theta);
    
        fprintf(fid,'\n');
    end
end
% fid = fopen('test.txt', 'wt');
% fprintf(fid,'%6.2f\n', test2.bbox);
fclose(fid);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值