matlab批量保存绘制的图像

clc, clear, close all

%循环读取文件夹下的所有txt
% 获取当前工作目录  
currentDir = pwd;  
% 定义相对路径  (这个没有使用)
relativePath = 'recordData/'; % 相对路径示例,替换为你的实际路径  
% 生成完整路径  (这个没有使用)
folderPath = fullfile(currentDir, relativePath); 
%这里就是导入数据的文件夹绝对路径
folderPath="D:\VsWorkspace\MatlabProject\Tag\14k"; 
% 获取文件列表  
%files = dir(fullfile(folderPath, '*.txt'));  % 获取所有.mat文件  
files=dir(fullfile(folderPath, '**', '*.txt'));
 % 指定保存路径  
SavePath = 'C:\Users\Lenovo\Desktop\测试图像\';  % 替换为你的文件夹保存路径 
for k = 1:length(files)  
    close all
    clearvars -except files folderPath k SavePath
    % 获取文件名  
    fileName = files(k).name;  
    filePath = fullfile(files(k).folder, fileName);  % 完整文件路径  



    %这里导入的文件数据
    wav = load(filePath);
    wav = reshape(wav', 1, []);
    plot(wav);
    






    %保存图像操作
    relativePath = strrep(files(k).folder, folderPath, '');   
    newFolderPath = fullfile(SavePath, relativePath);  
    if exist(newFolderPath, 'dir')  
        disp('文件夹已存在。');  
    else  
        mkdir(newFolderPath);  % 创建文件夹  
        disp('文件夹创建操作。');  
    end
    % 使用 fileparts 去掉后缀  
    [~, name, ~] = fileparts(fileName);  % name = 'data1'  
    % 添加新的后缀  
    newFileName = [name, '.png'];  % newFileName = 'data1.png'
    % 自动保存图形  
    saveas(gcf, fullfile(newFolderPath, newFileName));  % 使用fullfile确保路径的正确性

end
disp("操作已经完成");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值