I have the below code. I want to save the file in bmp format.
clearvars
indir = '.'; %current directory
outdir = 'C:\Users\gagan\Downloads\testing_lab\Stage-1 testing\plots'; %where to write the results
files = dir( fullfile(indir, '*.csv'));
for file = files'
inname = fullfile(file.folder, file.name);
n = readmatrix(inname);
m = rescale(n, -1, 1, 'InputMin',2301,'InputMax',3642)+0.527;
[filepath,name,ext] = fileparts(inname);
outname = fullfile(outdir, name + '.bmp');
plot(m) ; % use your variable in the plot
saveas(gcf,outname)
end
But when I run the below code I get error as:
Arrays have incompatible sizes for this operation.
Error in automated_making_plots (line 11)
outname = fullfile(outdir, name + '.bmp');
So, to resolve, I modify the below two line in the code above as:
outname = fullfile(outdir, name);
saveas(gcf,outname,'bmp')
Now, my code runs but the files that it saves is not in the bmp format but MATLAB is instead taking the string after the "." as the fomat. For example my file is "T15.11.02.38.csv", so instead of taking name as "T15.11.02.38.bmp", it creates file but with extension ".38".
Can someone please help? Thanks

 Respuesta aceptada

Image Analyst
Image Analyst el 17 de Mzo. de 2022

0 votos

Try this:
clearvars
indir = '.'; %current directory
outdir = 'C:\Users\gagan\Downloads\testing_lab\Stage-1 testing\plots'; %where to write the results
files = dir( fullfile(indir, '*.csv'));
for k = 1 : length(files)
inname = fullfile(files(k).folder, files(k).name);
n = readmatrix(inname);
m = rescale(n, -1, 1, 'InputMin',2301,'InputMax',3642)+0.527;
% Now plot.
plot(m) ; % use your variable in the plot
% Now that you have a figure up, save the figure window
[~, nameNoExt, ext] = fileparts(inname);
fullOutputFileName = fullfile(outdir, [nameNoExt, ext]);
exportgraphics(gcf, fullOutputFileName);
end

5 comentarios

Giggs B.
Giggs B. el 17 de Mzo. de 2022
Hi, I get this error:
Error using exportgraphics
File format 'csv' is not valid for export.
Error in test_code (line 14)
exportgraphics(gcf, fullOutputFileName);
Image Analyst
Image Analyst el 17 de Mzo. de 2022
Sorry, you need to change the extension
fullOutputFileName = fullfile(outdir, [nameNoExt, '.bmp'])
Giggs B.
Giggs B. el 17 de Mzo. de 2022
I am still getting error with bmp, I even tried png. But same error:
Error using exportgraphics
File format 'bmp' is not valid for export.
Error in test_code (line 14)
exportgraphics(gcf, fullOutputFileName);
Image Analyst
Image Analyst el 17 de Mzo. de 2022
Well you shouldn't be using BMP anyway - it's nearly obsolete. Use .PNG instead.
Giggs B.
Giggs B. el 17 de Mzo. de 2022
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 17 de Mzo. de 2022

Comentada:

el 17 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by