Dynamically rename existing structure arrays

Hi all,
I have several .mat files, each of them containing one structure array named "data" (named exactly the same in each .mat file), like the following
data =
hdr: [1x1 struct]
label: {306x1 cell}
time: {1x25 cell}
and my ultimate aim is to merge all those "data" arrays using a function (ft_appendata) of the Fieldtrip toolbox.
In order to do that, I need to have all those "data" arrays named differently from each other.
What I want to do is to create a loop which loads the files and dynamically renames the "data" arrays.
For example, having the files...
1_myfile_1.mat
1_myfile_2.mat
2_myfile_1.mat
2_myfile_2.mat etc...
...I want to load all the files and rename each "data" variable contained in each .mat file...
for s=1:2
for r=1:2
load([num2str(s),'_myfile_',num2str(r),'.mat'])
%here piece of code, which I don't know, needed for renaming the variable
end
end
...in order to get something like...
1_data_1 =
hdr: [1x1 struct]
label: {306x1 cell}
time: {1x25 cell}
1_data_2 =
hdr: [1x1 struct]
label: {306x1 cell}
time: {1x19 cell}
2_data_1 =
hdr: [1x1 struct]
label: {306x1 cell}
time: {1x16 cell}
etc.
I tried to use the eval() function but, as far as I have understood, it generates from scratch a new variable dynamically changing name at each iteration, while I need to simply rename an already existing structure array.
Thank you in advance for your kind help, Chiara

 採用された回答

Image Analyst
Image Analyst 2014 年 7 月 28 日
編集済み: Image Analyst 2014 年 7 月 28 日

0 投票

Don't do that. Put them into an array instead.
% Read all data into different cells of a cell array
allData = cell(2, 2); % Initialize to whatever size you need
folder = pwd; % Current folder or wherever you want.
for s = 1 : size(allData, 2)
for r = 1 : size(allData, 1)
baseFileName = sprintf('%d_myfile_%d.mat', s, r);
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
storedStructure = load(fullFileName)
% Put into cell at row r, column s:
ca(r, s) = {storedStructure.data};
end
end
end
A structure array would also work. These methods are preferred over what you asked for. Why? Read the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

1 件のコメント

Chiara Avancini
Chiara Avancini 2014 年 7 月 28 日
編集済み: Chiara Avancini 2014 年 7 月 28 日
Thank you! this worked nicely and it is also a pretty flexible solution for other types of data.

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

その他の回答 (2 件)

Michael Haderlein
Michael Haderlein 2014 年 7 月 28 日

0 投票

It's not necessary to rename your variables. I suppose you want a matrix with all the structure, right? Then, the following should work:
alldata=struct('hdr',cell(2),'label',[],'time',[]);
for s=1:2
for r=1:2
load([num2str(s),'_myfile_',num2str(r),'.mat'])
alldata(s,r)=data;
end
end
dpb
dpb 2014 年 7 月 28 日
編集済み: dpb 2014 年 7 月 28 日

0 投票

Just concatenate them as you read the files...
alldat=[]; % an empty holder
for s=1:2
for r=1:2
alldat=[alldat;load([num2str(s),'_myfile_',num2str(r),'.mat'])]; % concatenate each
end
end
ADDENDUM
If don't have a huge number this shouldn't be too bad altho it's generally not "best practice" to dynamically grow arrays. Not sure w/ structures what happens if create an empty structure array vis a vis concatenation; never tested it.

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by