Variables under the same name in different .mat files
27 ビュー (過去 30 日間)
古いコメントを表示
I have 11 different .mat files saved that contain the same variables. The variables have the same name and i want to create a .mat file that contains all of those variables separately. For example, say the .mat file name is FILE1.mat and contains variable A and then i have FILE2.mat that contains variable A but with different content. I want to create FILE.mat with variables A1 (From FILE1) and A2 (from FILE2). Any ideas?
1 件のコメント
Stephen23
2020 年 8 月 7 日
Forcing numbers into variable names is a sign that you are doing something wrong.
Instead of making your data harder to access, you should just use one simple non-scalar structure. Then you can trivially and efficiently access your data using basic indexing:
回答 (2 件)
Star Strider
2020 年 8 月 7 日
Use load to load each of the files into a different structure. See Load List of Variables into Structure Array.
You can then identify and rename the variables to save as different variables in the new .mat file.
0 件のコメント
Stephen23
2020 年 8 月 7 日
編集済み: Stephen23
2020 年 8 月 7 日
Rather than loading into separate structures and creating numbered variables (which is a bad way to write code), you should simply load the data and create one structure array which you can trivially accessing using basic indexing:
N = number of files
C = cell(1,N);
for k = 1:N
C{k} = load(...,'A');
end
S = [C{:}]; % create structure array
Then you can simply and efficiently access its data:
S(1).A
S(2).A
S(3).A
...
and of course saving it is easy too:
save('myfile.mat','S')
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!