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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by