How Can i write values in excel row by row??

5 views (last 30 days)
Sagar Sawant
Sagar Sawant on 26 Mar 2017
Answered: Guillaume on 26 Mar 2017
can anyone tell me how am i supposed to write values to excel but in row by row manner i have 8 images and i have calculated the energy,entropy,dissimilarity,auto-corelation,contrast now i have stored them in a,b,c,d,e respectively and then in x=[a b c d e] so i want to write these values row by row in cell.And they are different since i am processing 8 images from a folder.The code i have written writes only the last value 8 times row by row in the excel sheet. please i need urgent help..thanks in advance.
Code:
srcFiles = dir('C:\Users\sagar\Desktop\Abnormal\*.jpg'); % the folder in which ur images exists
for i = 1 : length(srcFiles)
filename = strcat('C:\Users\sagar\Desktop\Abnormal\',srcFiles(i).name);
I = imread(filename);
% figure, imshow(I);
t = rgb2gray(I);
w = adapthisteq(t);
GLCM2 = graycomatrix(w);
v = GLCM_Features1(GLCM2,0);
a = v.autoc
b = v.contr
c = v.dissi
d = v.energ
e = v.entro
x = [a b c d e]
offset = 1;
for a_iter = 1:8
xlswrite('newdata6.xls',x,1,sprintf('A%d',offset));
offset = offset + 1;
end
end

Answers (1)

Guillaume
Guillaume on 26 Mar 2017
Note: Any time you start numbering variables or naming them sequentially you should stop and rethink what you're doing. These variable are obviously related and therefore should be stored in just one variable as a matrix, cell array or other kind of container.
In you case, the simplest is to keep them as they are, as a structure. And rather than trying to write the result one row at a time (which is possible but slower), write it all at once at the end. I would use tablewrite instead of xlswrite as it's more powerful.
srcFiles = dir('C:\Users\sagar\Desktop\Abnormal\*.jpg'); % the folder in which ur images exists
glcmfeatures = [];
for fileindex = 1 : numel(srcFiles)
img = imread(fullfile('C:\Users\sagar\Desktop\Abnormal', srcFiles(fileindex).name));
grayimg = rgb2gray(img);
equalisedimg = adapthisteq(grayimg);
GLCM2 = graycomatrix(equalisedimg);
glcmfeatures = [glcmfeatures; GLCM_Features1(GLCM2,0)]; %#ok<AGROW> concatenate structure with ones from previous loop
end
writetable(struct2table(glcmfeatures), 'newdata6.xls');
Note that I've renamed all your variables to something a lot more meaningful. I'm growing a structure array here. For better performance, you could preallocate it.

Categories

Find more on Structures 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!