Convert the cell array to matrix using for loop

조회 수: 2 (최근 30일)
isra sahli
isra sahli 2021년 12월 8일
편집: Adam Danz 2021년 12월 9일
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

답변 (1개)

Adam Danz
Adam Danz 2021년 12월 8일
편집: Adam Danz 2021년 12월 9일
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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by