Populating an array through a loop
1 view (last 30 days)
Show older comments
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. ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1108075/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1108075/image.jpeg)
% How many unique phase IDs are in the dataset?
u = unique(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.
0 Comments
Answers (1)
Cris LaPierre
on 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
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!