How to access data in my cell array?

2 vues (au cours des 30 derniers jours)
Nathan McMahon
Nathan McMahon le 16 Mar 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 commentaire
Matt J
Matt J le 16 Mar 2023
What is the desired result in your posted example?

Connectez-vous pour commenter.

Réponse acceptée

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 16 Mar 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 commentaires
Nathan McMahon
Nathan McMahon le 16 Mar 2023
Thank you
Sulaymon Eshkabilov
Sulaymon Eshkabilov le 16 Mar 2023
Most welcome! Glad to help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by