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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by