How do I access cell array using names contained in master cell array?

7 ビュー (過去 30 日間)
Bryan Mackey
Bryan Mackey 2023 年 9 月 20 日
コメント済み: Bryan Mackey 2023 年 9 月 20 日
I have variable data sets that will contain one each of up to 224 individual (varying in number of elements) cell arrays. I can check the existance and create a "master" cell array where each element contains the name of the individual cell arrays. I want to loop through the master list and access the contents of the individual cell arrays.
Ex: master cell array = {'AA_label','AB_label','AC_label','AD_label'....} where 'AA_label' exists and has 8 elements, 'AD_label' exists and has 12 elments....
  4 件のコメント
Stephen23
Stephen23 2023 年 9 月 20 日
"Challenge is data set is being fed to me from another source as a cell array matched to a double matrix but I won't know which one is given until provided."
You forgot to tell us the most important information: how do all of these variables get into the workspace? Variables must be created somehow, and that is the correct place to fix this bad data design. For example if you import that data from some MAT files, then simply LOAD into an output variable and access its fields:
S = load(..)
Solve the actual problem (bad data import or similar) rather than doing this:
Bryan Mackey
Bryan Mackey 2023 年 9 月 20 日
Thanks...I had a total brain freeze....forgot S = load(...)

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2023 年 9 月 20 日
編集済み: Bruno Luong 2023 年 9 月 20 日
First thing to do is to convert your data to struct or table or whatever suitable for your data.
mystruct = struct();
for fc = mastercell(:)'
f = fc{1};
mystruct.(f) = eval(f);
end
save('myfile.mat', 'mystruct');
Then later load myfile and use mystruct and forget about your master and variables AA***
Alternatvely you might
  • read your data,
  • clear all workspace excepted those data
eval(['clearvars -except mastercell ' sprintf('%s ', mastercell{:})])
  • save the workspace in file,
save ws.mat
  • then later load it as struct.
s = load('ws.mat');
% use s ...

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by