how to make loop to save value in excel without overwrite with the previous value filled?

4 ビュー (過去 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

回答 (1 件)

Abolfazl Chaman Motlagh
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 件のコメント
nurul atikah mohd sharif
nurul atikah mohd sharif 2022 年 2 月 22 日
thank you so much for your help.
I've try the code, but it give me 'Error using writecell (line 127), Invalid parameter name: WriteMode.'
I found that this is due to version of matlab, WriteMode available in latest version, mine only R2019b.
Could you please suggest what can be done to append data to an existing sheet in the previous versions?
Abolfazl Chaman Motlagh
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 ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by