How to access data in my cell array?

2 Ansichten (letzte 30 Tage)
Nathan McMahon
Nathan McMahon am 16 Mär. 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 Kommentar
Matt J
Matt J am 16 Mär. 2023
What is the desired result in your posted example?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 16 Mär. 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 Kommentare
Nathan McMahon
Nathan McMahon am 16 Mär. 2023
Thank you
Sulaymon Eshkabilov
Sulaymon Eshkabilov am 16 Mär. 2023
Most welcome! Glad to help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by