How to access data in my cell array?

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 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);

Più risposte (0)

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by