How to access data in my cell array?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nathan McMahon
am 16 Mär. 2023
Kommentiert: Sulaymon Eshkabilov
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
Akzeptierte Antwort
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!