I have a folder with multiple txt files that I want to print into separate figures. How can I load, plot, and print each figure with the same file name without naming them manually each time?

 Akzeptierte Antwort

Image Analyst
Image Analyst am 25 Dez. 2022

0 Stimmen

Is this what you want to do?
% Get a list of all txt files in the current folder.
fileList = dir('*.txt')
numFiles = numel(fileList)
% Create a figure for each file.
for k = 1 : numFiles
% Build the string you want to display in the titlebar of the figure.
thisFileName = sprintf('File #%d of %d = %s', k, numFiles, fileList(k).name)
% Create a figure and make the title bar have that string.
hFig(k) = figure('Name', thisFileName, 'NumberTitle','off');
% Now do stuff with thisFileName, like plot it or whatever
end

4 Kommentare

usually I would use somthing like
A = load('Data1.txt');
figure (1)
title('sample1')
xlabel('Time')
ylabel('delta (unit)')
hold on
plot (A(:,1),A(:,2), 'LineWidth',1, 'Color', [0 0.4470 0.7410])
plot (A(:,1),A(:,3), 'LineWidth',1, 'Color', [0.6350 0.0780 0.1840])
print(gcf,'Data1.png','-dpng','-r600');
but this time I have too many data files and neet it to work on a loop, I couldn't do it without errors
Image Analyst
Image Analyst am 26 Dez. 2022
Bearbeitet: Image Analyst am 26 Dez. 2022
I think this should work:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Get a list of all txt files in the current folder.
fileList = dir('*.txt')
numFiles = numel(fileList)
% Create a figure for each file.
for k = 1 : numFiles
% Build the string you want to display in the titlebar of the figure.
thisFileName = sprintf('File #%d of %d = %s', k, numFiles, fileList(k).name)
% Create a figure and make the title bar have that string.
hFig(k) = figure('Name', thisFileName, 'NumberTitle','off');
% Now do stuff with thisFileName, like plot it or whatever
A = load(thisFileName) % A is a structure here.
% Extract from structure into vectors.
t = A.x;
y1 = A.y;
y2 = A.z;
figure;
plot (t, y1, 'LineWidth',1, 'Color', [0 0.4470 0.7410])
hold on
plot (t, y2, 'LineWidth',1, 'Color', [0.6350 0.0780 0.1840])
caption = sprintf('Data from %s', thisFileName);
title(caption)
xlabel('Time')
ylabel('Delta (units)')
[folder, baseFileNameNoExt, ext] = fileparts(thisFileName);
outputFileName = fullfile(folder, [baseFileNameNoExt, '.png']);
fprintf('Writing %s\n', outputFileName);
% print(gcf,outputFileName,'-dpng','-r600');
exportgraphics(gcf, outputFileName);
end
Make sure you're extracting the correly named field from the "A" structure or else it won't work.
Mohammed Alhamdan
Mohammed Alhamdan am 27 Dez. 2022
Bearbeitet: Mohammed Alhamdan am 27 Dez. 2022
Thank you this helped. I used it to write the following and it worked
clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
fileList = dir('*0001.txt')
numFiles = numel(fileList)
for k = 1 : numFiles
FileName = sprintf('%s', fileList(k).name)
A = load(FileName);
figure;
title(FileName);
xlabel('Time delay (ps)')
ylabel('delta T/ T (arb .unit)')
hold on
plot (A(:,1),A(:,2), 'LineWidth',1, 'Color', [0 0.4470 0.7410])
plot (A(:,1),A(:,3), 'LineWidth',1, 'Color', [0.6350 0.0780 0.1840])
print(gcf, fileList(k).name+".png");
end
now my only problem is with [ title(FileName) ]. My files names contain underscores ( _ ) instead of spaces (for example: Data_22-12-23_1534_0001.txt), and this create small letter after each underscore on the title on my figure. is there a solution for it?
screenshot of title on figure:
Use the 'Interpreter' 'none' option
title(FileName, 'Interpreter', 'none', 'FontSize', 20);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by