Populating an array through a loop

3 Ansichten (letzte 30 Tage)
Hannah Cunningham
Hannah Cunningham am 25 Aug. 2022
Beantwortet: Cris LaPierre am 25 Aug. 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.

Antworten (1)

Cris LaPierre
Cris LaPierre am 25 Aug. 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

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by