Convert the cell array to matrix using for loop

8 views (last 30 days)
isra sahli
isra sahli on 8 Dec 2021
Edited: Adam Danz on 9 Dec 2021
I have a cell array that size 32*8 i want to convert to matrix, i use this code and have this problem (Unable to perform assignment because the indices on the left side are not compatible with the size of the right side) how can i solve
note: the number_of_collecting_fibers is less than 32
the code:
number_of_collecting_fibers=str2num(get(handles.numberoffibers,'string')) % the number of collecting fibers
Dataofcollectingfibers=get (handles.collectingfibersdatatable, 'data') %the data of the table
hold on;
for i=1:number_of_collecting_fibers
for j=1:8
Data_of_collecting_fibers(i,j) = cell2mat(Dataofcollectingfibers(i,j))
end
end

Answers (1)

Adam Danz
Adam Danz on 8 Dec 2021
Edited: Adam Danz on 9 Dec 2021
Since you're dealing with scalars, instead of this line
Data_of_collecting_fibers(i,j) = cell2mat(Dataofcollectingfibers(i,j))
you should use
Data_of_collecting_fibers(i,j) = Dataofcollectingfibers{i,j};
Now to the main problem. Some of your cells are empty. Converting them from cell to matrix results in {}-->[] which has a sizes of (0,0) yet you're trying to store them in a container of size (1,1). Matries cannot have empty values.
Solution: replace the empties with NaN values which are placeholders for empty numeric values.
Dataofcollectingfibers(cellfun('isempty',Dataofcollectingfibers)) = {NaN};
then proceed with the cell-to-mat conversion described above.
Alternatively, you could check each value individually,
for j = 1:8
if isempty(Dataofcollectingfibers{i,j})
Data_of_collecting_fibers(i,j) = NaN;
else
Data_of_collecting_fibers(i,j) = Dataofcollectingfibers{i,j};
end
end

Tags

Community Treasure Hunt

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

Start Hunting!