Figures not saving as specific file type regardless of specified extension

4 visualizaciones (últimos 30 días)
I am running some code that runs through a directory, takes data from the files, plots the required data form each, and then saves the figure into the same folder before moving onto the next file. The code and processes work fine with the correct data being taken, only when going to the directory where the figures were saved, their extension shows as some number 'file'. e.g. '2 file', '25 file' etc. I can open the fiigure file in paint, but would prefer the file to be saved as what I specified. Here is the code I'm using;
clear all
close all
clc
filedirectory = 'C:\Users\Me\OneDrive\Documents\University\Rogue Waves\Part1\Initial Conditions\Practise\';
filepattern = fullfile(filedirectory, '*.txt');
files = dir(filepattern);
for k = 1:length(files)
filename = files(k).name;
fullfilename = fullfile(filedirectory, filename);
fprintf(1, 'Running simulation for %s\n', fullfilename);
%process data ...
figure, pcolor(tt,xx,abs(u_arr))
xlabel(['Time (s)']);ylabel('Evolution Lab Space (m)');title('Numerics')
shading interp%title(['Nonlinear Time is ' num2str(T_NL,3) ' s, Soliton Period is ' num2str(pi/2*T_D,3) ' s' ])
colormap(jet)
colorbar
str = strcat('Numerics', filename);
savename = strrep(str,'.txt','');
%save the figure in folder
saveas(gcf, fullfile(filedirectory, savename), 'png')
close(gcf)
end
Example of the file extension:

Respuesta aceptada

Stephen23
Stephen23 el 16 de Oct. de 2019
Editada: Stephen23 el 16 de Oct. de 2019
Specifying the image file type and specfiying the file extension are two totally different things. Your title "Figures not saving as specific file type regardless of specified extension" indicates that you confuse the two of them: in fact your files are saved with exactly the file type that you specified, and you did not explicitly specify any file extension. Just because the extension (if any) is not what you are used to seeing means nothing about the file type: does changing a .jpg file extension to .doc turn the file into a Word document? No, all you will get is the same file (whatever it may be) with a different extension.
Your filenames includes period characters, which means that the part of the filename folliowing the final period character is interpreted as the file extension (e.g. .25). Thus this part of the saveas documentation is relevant: "If you specify a file extension, it does not have to match the format"
If you want to save a file with a particular extension, then you need to specify it, e.g.:
[~,fnm] = fileparts(filename);
new = sprintf('Numerics_%s.png',fnm);
saveas(gcf, fullfile(filedirectory,new), 'png');
  1 comentario
Liam Guest
Liam Guest el 16 de Oct. de 2019
Oh of course, makes complete sense why the file type was matching the .# part of the file. Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by