how to make loop to save value in excel without overwrite with the previous value filled?
조회 수: 3 (최근 30일)
이전 댓글 표시
Can anyone help me with this code, I keep on tring to get result with a list of value of histogram bincount by channel into one excel with different sheet and no overwrite. Four your information, in myFolder contain 25 images. So it should be 25 rows of output for each sheet with 256 columns.
%% Step 1: Read Image
% Specify the folder where the files live.
myFolder = 'C:\REFERENCE IMAGE';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Separate RGB Channel
A = imread(fullFileName);
Ro = A(:,:,1);
Go = A(:,:,2);
Bo = A(:,:,3);
%% Step 2: Histogram
filename = 'Histogram.xlsx';
% Ro
[counts,~] = imhist(Ro);
DATAR = {counts};
% Go
[counts,~] = imhist(Go);
DATAG = {counts};
% Bo
[counts,~] = imhist(Bo);
DATAB = {counts};
%% Step 3: Save each bin value from each image into excel
for D = [DATAR, DATAG, DATAB]
cellReference = sprintf('%d',str2double(D));
xlswrite(filename, DATAR, 'Ro', cellReference);
xlswrite(filename, DATAG, 'Go', cellReference);
xlswrite(filename, DATAB, 'Bo', cellReference);
end
end
댓글 수: 0
답변 (1개)
Abolfazl Chaman Motlagh
2022년 2월 16일
for problem you mentioned maybe use writematrix function instead, and use option 'WriteMode' = 'append'.
writematrix(DATAR,filename,'Sheet','Ro','WriteMode','Append');
or you can use writecell exactly the way you use writematrix.
but i'm not sure your doing well at all. the DATAR for example is a cell of all bins returned by imhist. for example a single cell containg an array with size of 256x1. so the part
for D = [DATAR, DATAG, DATAB]
runs 3 times, first time D is DATAR, second time is DATAG and ... .
but you treated D differently.
i think this is what you meant :
%RO
[counts,~] = imhist(Ro);
DATAR = {counts};
%GO
[counts,~] = imhist(Go);
DATAG = {counts};
% Bo
[counts,~] = imhist(Bo);
DATAB = {counts};
writecell(DATAR,filename,'Sheet','Ro','WriteMode','Append');
writecell(DATAG,filename,'Sheet','Ro','WriteMode','Append');
writecell(DATAB,filename,'Sheet','Ro','WriteMode','Append');
with this code you'll have a 25 row with 256 number from hist function in each one.
be carefull using writematrix for your problem, because counts in 256x1 array and each time you append it to xls file the number of row increase by 256 so after 25 image you just have 25x256 row and 1 column. you can transpose the array and then write it to the file for this problem.
댓글 수: 2
Abolfazl Chaman Motlagh
2022년 2월 22일
i don't have the version to test. but doesn't writematrix that i suggest first work neither?
of course if nothing works. there is one dirty solution :)
you can read file first, append your data to that just readed variable and write whole variable again in the file. it will append the data but not in a clear way.
but it seems someone provide this solution in fileexchange: xlsappend
if it doens't work eaither for you. tell so i can provide a better solution for your version.
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!