Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何将数据集中的mat文件转换为TXT文件 #72

Open
jone222 opened this issue Feb 18, 2024 · 2 comments
Open

如何将数据集中的mat文件转换为TXT文件 #72

jone222 opened this issue Feb 18, 2024 · 2 comments

Comments

@jone222
Copy link

jone222 commented Feb 18, 2024

No description provided.

@mpmmpmmmp
Copy link

mpmmpmmmp commented Feb 20, 2024

以下是一个例子, 你需要根据x, y坐标的对应位置决定是否需要交换x和y坐标的位置

root_folder = '';

% 获取根文件夹中所有子文件夹的列表
subfolders = dir(root_folder);
subfolders = subfolders([subfolders.isdir] & ~strcmp({subfolders.name}, '.') & ~strcmp({subfolders.name}, '..'));

% 遍历每个子文件夹
for s = 1:length(subfolders)
subfolder_path = fullfile(root_folder, subfolders(s).name, 'mats');

% 获取子文件夹中所有.mat文件的列表
mat_files = dir(fullfile(subfolder_path, '*.mat'));

% 获取.txt文件保存的路径
output_folder = fullfile(fileparts(subfolder_path), 'txt');
if ~exist(output_folder, 'dir')
    mkdir(output_folder);
end

% 循环处理每个.mat文件
for i = 1:length(mat_files)
    % 读取.mat文件
    file_path = fullfile(subfolder_path, mat_files(i).name);
    data = load(file_path);
    
    % 提取image_info.location
    locations = data.image_info.location;
    
    % 交换x和y坐标的位置
    swapped_locations = [locations(:,2), locations(:,1)];
    
    % 获取.mat文件的基本文件名(不包括扩展名)
    [~, base_name, ~] = fileparts(mat_files(i).name);
    
    % 构建.txt文件的保存路径
    output_file_path = fullfile(output_folder, [base_name, '.txt']);
    
    % 打开输出的文本文件
    output_file = fopen(output_file_path, 'w');
    
    % 写入.txt文件
    fprintf(output_file, '%f %f\n', swapped_locations');
    
    % 关闭输出文件
    fclose(output_file);
end

end

@jone222
Copy link
Author

jone222 commented Feb 20, 2024

感谢,我尝试一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants