Hi Viet
I understand you are trying to create figures, save them as images, and then calculate some properties of these images to save the results in an Excel file. Some enhancements to implement in existing code are:
- Path Management: Make sure to use the correct paths when saving and reading files. Use “fullfile” to construct paths.
- Image Saving and Reading: Ensure to correctly save and read the images. The path should be consistent.
- Excel File Saving: Use the correct path when saving the Excel file within the newly created directories.
- Loop and Path Variables: Ensure the variables used in paths are correctly defined.
Here is the enhanced code following the same:
% Example of creating a plot and storing its handle in 'aa'
[x, y, z] = sphere; % Create a sphere for demonstration
figure;
aa = surf(x, y, z); % Create a surface plot and store the handle in 'aa'
for ii = 0:10:360
for jj = 0:10:90
view(ii, jj);
% Create a multiple folder
newdir = sprintf('view%02dview%02d', ii, jj);
mkdir(newdir);
% Initialize array to store red areas
redArea = zeros(1, 36);
% Rotate masks and save figures
for i = 1:36
rotate(aa, [0, 0, 1], 1);
% Print figure with rotation
filename = sprintf('Slice_%d.png', i);
filepath = fullfile(newdir, filename);
print('-dpng', filepath);
pause(2);
end
% Calculate red pixels and save in Excel file
for i = 1:36
filename = sprintf('Slice_%d.png', i);
filepath = fullfile(newdir, filename);
rgbImage = imread(filepath);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Calculate red pixels
redPixels = redChannel == 255 & greenChannel == 0 & blueChannel == 0;
redArea(i) = sum(redPixels(:));
end
% Save redArea results to an Excel file
excelFilename = fullfile(newdir, 'test.xlsx');
xlswrite(excelFilename, redArea');
end
end
Hope that helps!