Hi, anybody can help me from this code,
I want to save all peaksnr output into one .csv using writematrix, but this code only save one value into the .csv.
Is there any method i can use to make sure all the output save into one csv.
%% Here is my code
indir = uigerdir(cd, 'Select input folder');
directory = dir([indir, '\', '*.jpg']);
for i = 1 : length(directory)
filename = directory(i).name;
ref = imread([indir, '\', filename]);
A = imnoise(ref, 'salt & pepper', 0.02);
[peaksnr, snr] = psnr(A, ref);
fprintf('\n Peak-SNR Original : %0.4f',peaksnr);
fprintf('\n SNR Original : %0.4f \n',snr);
writematrix(peaksnr, sprintf('psnrO.csv',i+1))
end
Thank you for your help.

 채택된 답변

Nur Farahin
Nur Farahin 2023년 1월 9일

0 개 추천

But, i've tried another solution an work for me as well.
indir = uigetdir(cd, 'Select input folder');
directory = dir([indir, '\', '*.jpg']);
for i = 1 : length(directory)
filename = directory(i).name;
ref = imread([indir, '\', filename]);
A = imnoise(ref, 'salt & pepper', 0.02);
[peaksnr, snr] = psnr(A, ref);
fprintf('\n Peak-SNR Original : %0.4f',peaksnr);
fprintf('\n SNR Original : %0.4f \n',snr);
writematrix(peaksnr,'psnrO.csv','WriteMode','append');
writematrix(snr,'snrO.csv','WriteMode','append');
end
just add the writemode and append argument, then the values will save all in one csv.

추가 답변 (2개)

VBBV
VBBV 2023년 1월 9일
편집: VBBV 2023년 1월 9일

0 개 추천

writematrix(peaksnr, 'psnrO.xls','Sheet', i)

댓글 수: 5

VBBV
VBBV 2023년 1월 9일
Use the sheet argument in writematrix function. Writematrix function writes to appropriate file name specified as 2nd argument.sprintf is not necessary in this case.
Nur Farahin
Nur Farahin 2023년 1월 9일
If im not mistaken, the sheet argument is used with the .xcel method isnt. I already try put the sheet argument by have an error the sheet argument.
CSV files are text files and do not have "sheets". I think @VBBV meant
writematrix(peaksnr, 'psnrO.xlsx','Sheet', i)
VBBV
VBBV 2023년 1월 9일
편집: VBBV 2023년 1월 9일
Yes, sheet argument is used with *.xls files. Once saved into Excel file, you can convert it to CSV file using saveas option. Is there a constraint for you to write only to csv files ?, BTW you can use delimiter option in writematrix function too, if the data to be written has text
Nur Farahin
Nur Farahin 2023년 1월 9일
Thank you for the answer.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2023년 1월 9일

0 개 추천

Try (untested)
% Ask user for folder.
indir = uigetdir(cd, 'Select input folder');
directory = dir(fullfile(indir, *.jpg'));
% Preallocate array for results.
peaksnr = zeros(length(directory), 1);
% Loop over all files, adding noise and then computing Peak SNR and SNR
for k = 1 : length(directory)
% Get this filename.
filename = fullfile(indir, directory(k).name);
% Read in image file from disk.
originalImage = imread(filename);
% Add noise
noisyImage = imnoise(originalImage, 'salt & pepper', 0.02);
% Compute SNR
[thisPSNR, thisSnr] = psnr(noisyImage, originalImage);
% Update progress in command window.
fprintf('\nPeak-SNR Original for "%s" (#%d of %d): %0.4f', ...
directory(i).name, k, length(directory), thisPSNR);
fprintf('\nSNR Original : %0.4f \n', thisSnr);
% Save this value into the master array.
peaksnr(k) = thisPSNR;
end
% Save all the values.
outputFileName = fullfile(indir, 'Peak SNR.xlsx');
writematrix(peaksnr, outputFileName);

댓글 수: 3

Nur Farahin
Nur Farahin 2023년 1월 9일
Thank you for the asnwer. This work for me with a little adjustment on the code.
Image Analyst
Image Analyst 2023년 1월 9일
Uh, you threw away all the corrections and improvements I made to your code, like
  1. using fullfile(),
  2. saving all the values to a single array so that you don't have to slow down your code by calling writematrix twice inside the loop,
  3. better progress messages,
  4. more descriptive variable names,
  5. addition of lots of comments, etc.
OK, "not invented here" I guess.
Nur Farahin
Nur Farahin 2023년 1월 9일
I appreciated your improvement to my code, i am clearly understand very well. Thank you so much for helping me out. I am clearly understand very well and managed to run the code. Iam sorry if you feel your code is not invented. Hope you dont feel offended. Maybe someone else will need the code.

댓글을 달려면 로그인하십시오.

카테고리

질문:

2023년 1월 9일

댓글:

2023년 1월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by