Populating an array through a loop

7 visualizaciones (últimos 30 días)
Hannah Cunningham
Hannah Cunningham el 25 de Ag. de 2022
Respondida: Cris LaPierre el 25 de Ag. de 2022
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.

Respuestas (1)

Cris LaPierre
Cris LaPierre el 25 de Ag. de 2022
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

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by