how to remove error "Conversion to cell from uint8 is not possible." in the line R(k) = cell2mat(R(k));?

9 views (last 30 days)
clc;
close all;
clear;
myFolder='D:\WORK\major project';
m=input('Type the Number of Images to Process:');% Ask the amount of image to process
for k = 1:m
tiffFilename = sprintf('image%d.tiff', k);
fullFileName = fullfile(myFolder, tiffFilename);
if exist(fullFileName, 'file')
imageData = imread(fullFileName);
IMAGE(k)= mat2cell(imageData);
else
warningMessage = sprintf('Warning: image file does not exist:\n%s', fullFileName);%if file doesent exist sends an error message
uiwait(warndlg(warningMessage));
end
end
for k = 1:m
temp = cell2mat(IMAGE(k));
R(k) = mat2cell(temp(:,:,1)); %<1*1>cell containing matrix of size 256*256 uint8%
G(k) = mat2cell(temp(:,:,2));
B(k) = mat2cell(temp(:,:,3));
R(k) = cell2mat(R(k));
B(k) = cell2mat(B(k));
G(k) = cell2mat(G(k));
L(k)= horzcat(R(k),G(k),B(k));
%concatenation%
end
for k = 2:m
V = vertcat(L(k-1),L(k));
end

Answers (1)

Prathamesh
Prathamesh on 5 May 2025
I understand that you are getting an error “Conversion to cell from uint8 is not possible”.
This happens because you are trying to assign a numeric array from “cell2matinto a cell array element, or vice versa. You are using arrays like IMAGE(k) and R(k) as if they are cell arrays, but you did not initialize them as such. If you just want to store matrices, do not wrap them as cells unless you specifically want cell arrays.
“R(k) = cell2mat(R(k)); “
is problematic because R(k) is already a matrix, not a cell.
Below is a revised version of your code:
clc;
close all;
clear;
myFolder = 'D:\WORK\major project';
m = input('Type the Number of Images to Process: '); % Number of images
IMAGE = cell(1, m); % Preallocate as cell array
for k = 1:m
tiffFilename = sprintf('image%d.tiff', k);
fullFileName = fullfile(myFolder, tiffFilename);
if exist(fullFileName, 'file')
imageData = imread(fullFileName);
IMAGE{k} = imageData; % Store directly as cell
else
warningMessage = sprintf('Warning: image file does not exist:\n%s', fullFileName);
uiwait(warndlg(warningMessage));
end
end
R = cell(1, m);
G = cell(1, m);
B = cell(1, m);
L = cell(1, m);
for k = 1:m
temp = IMAGE{k};
R{k} = temp(:,:,1); % Store as cell
G{k} = temp(:,:,2);
B{k} = temp(:,:,3);
L{k} = horzcat(R{k}, G{k}, B{k}); % Concatenate and store as cell
end
% If you want to vertically concatenate all images:
V = L{1};
for k = 2:m
V = vertcat(V, L{k});
end

Categories

Find more on Encryption / Cryptography 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!