Clear Filters
Clear Filters

How to create .png files having same file name as the .dat file from which I'm writing the code?

2 views (last 30 days)
I'm having .dat files having name t-0.dat,t-1.dat,t-2.dat,t-4.dat,t-16.dat,.....t-100000.dat. I want to make .png files with the same names i.e. t-0.png,t-1.png,t-2.png,.....How do I do it?
Here is my code. Here I could create files with name- fig1.png,fig2.png,fig3.png,fig4.png.(fig1 corresponding to t-0.dat, like this. But I want to name it t-0.png or fig1.png).
myfolder= '........../datafolder';
datfiles = dir('*.dat');
for k = 1 : length(datfiles)
baseFilename= datfiles(k).name;
data = load(datfiles(k).name);
fullFilename= fullfile(myfolder, baseFilename);
fprintf(1, 'Now reading %s\n', fullFilename);
h=figure;
scatter(data(:,1),data(:,2),50);
hold on;
quiver(data(:,1),data(:,2),data(:,4),data(:,5),0.3);
saveas(h,sprintf('fig%d.png',k));
hold off;
end

Accepted Answer

Stephen23
Stephen23 on 22 May 2017
Edited: Stephen23 on 22 May 2017
[fpth,fnm] = fileparts(original_name);
new_name = fullfile(fpth,[fnm,'.png']);

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!