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

来源:互联网 发布:na.windows.app.23787 编辑:程序博客网 时间:2024/05/17 03:39

最近在做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_numfor 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');    endend% fid = fopen('test.txt', 'wt');% fprintf(fid,'%6.2f\n', test2.bbox);fclose(fid);


阅读全文
0 0
原创粉丝点击