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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/B08370108/article/details/77098966

最近在做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);


猜你喜欢

转载自blog.csdn.net/B08370108/article/details/77098966