Storing multiple value in array without overriding.

Below is the code snippet, where I am selecting images one by one, converting a image in 30*30 patch and calculating the standard deviation of patch and then storing that value in excel sheet. But the problem is only the standard deviation of last image is being saved to excel sheet others value are getting over ridden. How to avoid that?
Directory = 'With MA\';
%Imgs = dir(fullfile(Directory,'*.jpg'));
Imgs = dir(Directory);
nfiles = length(Imgs); % Number of files found
for z=1:nfiles
thisname = Imgs(z).name;
thisfile = fullfile(Directory, thisname);
try
Img = imread(thisfile);
%Img = imread(fullfile(Directory,Imgs(z).name));
I=Img(:,:,2);
imSz = size(I);
patchSz = [30 30];
xIdxs = [1:patchSz(2):imSz(2) imSz(2)+1];
yIdxs = [1:patchSz(1):imSz(1) imSz(1)+1];
patches = cell(length(yIdxs)-1,length(xIdxs)-1);%for patches
stan = cell(length(yIdxs)-1,length(xIdxs)-1);%for standard deviation
for i = 1:length(yIdxs)-1
Isub = I(yIdxs(i):yIdxs(i+1)-1,:);
for j = 1:length(xIdxs)-1
patches{i,j} = Isub(:,xIdxs(j):xIdxs(j+1)-1);%saving patches
stan{i,j} = std2(patches{i,j});%saving standard deviation
end
end
catch
end
xlswrite('output.xlsx', stan);
end

 Respuesta aceptada

Stephen23
Stephen23 el 11 de Mayo de 2019
Editada: Stephen23 el 11 de Mayo de 2019
"How to avoid that?"
Simply by changing the Excel worksheet name on each iteration of the outer loop, e.g.:
...
for z=1:nfiles
...
F = sprintf('output_%d.xlsx',z);
xlswrite(F,...);
end
Or you could use fileparts to create a name based on the original image filenames, e.g.:
[~,N] = fileparts(thisname);
F = sprintf('%s.xlsx',N);
Note that you should place the xlswrite inside the try section, otherwise your code will export the results from the last imported file, without any warning. Placing so much code inside try is not a good idea, as this hides any errors and bugs in that part of the code. Better code would simply use exist to test for the file (rather than try). I doubt that try does anything useful here anyway, as dir only lists existing files anyway, so most likely you should get rid of try entirely.
Read this:

4 comentarios

Where the saved .xlsx file will go? Because we are not providing any path, Even if I am providing the path, a warning is coming and nothing gets done.
Warning: Escaped character '\U' is not valid. See 'doc sprintf' for supported special characters.
Updated Code:
Directory = 'With MA\';
%Imgs = dir(fullfile(Directory,'*.jpg'));
Imgs = dir(Directory);
nfiles = length(Imgs); % Number of files found
for z=1:nfiles
thisname = Imgs(z).name;
thisfile = fullfile(Directory, thisname);
try
Img = imread(thisfile);
%Img = imread(fullfile(Directory,Imgs(z).name));
I=Img(:,:,2);
imSz = size(I);
patchSz = [30 30];
xIdxs = [1:patchSz(2):imSz(2) imSz(2)+1];
yIdxs = [1:patchSz(1):imSz(1) imSz(1)+1];
patches = cell(length(yIdxs)-1,length(xIdxs)-1);%for patches
stan = cell(length(yIdxs)-1,length(xIdxs)-1);%for standard deviation
for i = 1:length(yIdxs)-1
Isub = I(yIdxs(i):yIdxs(i+1)-1,:);
for j = 1:length(xIdxs)-1
patches{i,j} = Isub(:,xIdxs(j):xIdxs(j+1)-1);%saving patches
stan{i,j} = std2(patches{i,j});%saving standard deviation
end
end
F = sprintf('C:\Users\LENOVO\Desktop\With MA\output_%d.xlsx',z);
xlswrite(F,stan);
%xlswrite('output.xlsx', stan);
catch
end
end
Stephen23
Stephen23 el 11 de Mayo de 2019
Editada: Stephen23 el 11 de Mayo de 2019
The warning occurs because in the format string you escape the characters \U and \L and \D and \W , none of which are valid escaped characters. You also escape \o , which is valid for formatting octal numbers, but you do not supply an input value to use this format specifier.
You could resolve this by escaping the backslash itself or by supplying the path as an input argument, but I recommend to simply use fullfile:
F = sprintf('output_%d.xlsx',z);
xlswrite(fullfile(Directory,F),stan);
ALOK RAJ
ALOK RAJ el 12 de Mayo de 2019
Thank you so much Sir.
ALOK RAJ
ALOK RAJ el 14 de Mayo de 2019
Like I am saving standard deviation in xlsx file, in the same way if i save gamma value in other xlsx file and then I want to plot a graph between standard deviation vs gamma. How can I do that?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Mayo de 2019

Comentada:

el 14 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by