Populating an array through a loop

조회 수: 7 (최근 30일)
Hannah Cunningham
Hannah Cunningham 2022년 8월 25일
답변: Cris LaPierre 2022년 8월 25일
I am working on code that calculates grain sizes (and other things) of different mineral phases and I want it to spit out a table at the end of it with the mean, mode, etc of each phase. I am having trouble populating the "Phase List", which will be the row values of the final table. Green cells in image.
% How many unique phase IDs are in the dataset?
u = unique(grains.phase);
Unable to resolve the name 'grains.phase'.
% So how long is the list of phase ID's?
s = size(u,1);
phaselist = zeros(s,1);
%for loop to repeat each step for each phase (starts at 2 because 1 = not
%indexed grains
for j=2:s
phase = grains(grains.phase==u(j)).mineral;
%% calculations
phaselist(j,1) = {phase}; % ERROR HERE
end
The error I get is with the braces and it not populating the cell in phaselist with the phase, which is a character array. I have tried it several ways but get the following:
phaselist(j,1) = {phase};
Conversion to double from cell is not possible.
phaselist{j,1} = {phase};
Unable to perform assignment because brace indexing is not supported for variables of this type.
Any help is appreciated. Thank You.

답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 8월 25일
Why do you need to use braces at all? You create phaselist using zeros. You can assign without converting your values to cells first.
phaselist = zeros(5,1);
phase = 5;
phaselist(2,1) = phase
phaselist = 5×1
0 5 0 0 0

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by