Merge cell arrays in a loop

3 ビュー (過去 30 日間)
Veilchen1900
Veilchen1900 2017 年 8 月 15 日
コメント済み: Veilchen1900 2017 年 8 月 16 日
Hi, I'm loading cell arrays from a folder and want them to combine into one cell array. This should happen in a loop. The folders contain different number of cell arrays. The command to merge cell arrays is C4 = [C1; C2; C3] but how can I use it im my code?
Thanks a lot in advance!
if
% Select input folder
d = uigetdir('M:\P\','Select Input-folder');
% Change current folder
cd(d);
% List all .mat files.
list = dir('*.mat');
l = length(list);
for k = 1: length(list)
% Load .mat files.
load(list(k).name);
end

採用された回答

Stephen23
Stephen23 2017 年 8 月 15 日
編集済み: Stephen23 2017 年 8 月 16 日
The trick is to load each .mat file into a structure, which you can then access the fields of:
C = cell(1,numel(list);
for k = 1:numel(list)
S = load(list(k).name);
C{k} = S.field;
end
And avoid slow ugly code that creates or accesses lots of numbered variables: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
  1 件のコメント
Veilchen1900
Veilchen1900 2017 年 8 月 16 日
Thank you Stephen!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by