How to access data in my cell array?

2 visualizaciones (últimos 30 días)
Nathan McMahon
Nathan McMahon el 16 de Mzo. de 2023
Comentada: Sulaymon Eshkabilov el 16 de Mzo. de 2023
I have an array that has "nested" arrays inside it and would like to call the information inside those arrays, but whenever I try and access it I can only call the location of the arrays inside the larger cell array.
spx = [1*10^6; 1200; .08];
dpx = [10*10^6; 1500; .22];
frx = [8*10^6; 1800; .15];
bpx = [6*10^6; 1200; .06];
x = {spx;dpx;frx;bpx};
K = 3;
C = cell(K, 1);
[C{:}] = ndgrid(x);
array = cellfun(@(x){x(:)}, C);
array = [array{:}];
k = 1:length(array);
[YM1,rho1,zeta1] = array(k,1); % problem area
  1 comentario
Matt J
Matt J el 16 de Mzo. de 2023
What is the desired result in your posted example?

Iniciar sesión para comentar.

Respuesta aceptada

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 16 de Mzo. de 2023
If understood your question correctly, here is the corrected code:
spx = [1*10^6; 1200; .08];
dpx = [10*10^6; 1500; .22];
frx = [8*10^6; 1800; .15];
bpx = [6*10^6; 1200; .06];
x = {spx;dpx;frx;bpx};
K = 3;
C = cell(K, 1);
[C{:}] = ndgrid(x);
array = cellfun(@(x){x(:)}, C);
array = [array{:}];
for k = 1:length(array)
YM1{k} = array{k,1}; % Call the cell array by a cell # and assing to a new variable
rho1{k} = array{k,2};
zeta1{k} = array{k,3};
end
%% Convert to matrix
YM1_mat=cell2mat(YM1);
rho1_mat=cell2mat(rho1);
zeta1_mat=cell2mat(zeta1);
%% ALternative faster way of converting and obtaining the data from cell array called: array
A = cell2mat(array);
YM1_mat = A(:,1);
zero1_mat = A(:,2);
zeta1_mat = A(:,3);
  2 comentarios
Nathan McMahon
Nathan McMahon el 16 de Mzo. de 2023
Thank you
Sulaymon Eshkabilov
Sulaymon Eshkabilov el 16 de Mzo. de 2023
Most welcome! Glad to help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by