![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/385788/image.png)
Assign values of .mat files into matrix
17 ビュー (過去 30 日間)
古いコメントを表示
- silomodresults1.mat
- silomodresults2.mat
- silomodresults3.mat
- silomodresults4.mat
- silomodresults5.mat
- silomodresults6.mat
- silomodresults7.mat
- silomodresults8.mat
- silomodresults9.mat
- silomodresults10.mat
- silomodresults11.mat
- silomodresults12.mat
- silomodresults13.mat
- silomodresults14.mat
- silomodresults15.mat
- silomodresults16.mat
- silomodresults17.mat
- silomodresults18.mat
- silomodresults19.mat
- silomodresults20.mat
Dear all,
I have multiple .mat files (see attachment). I want to organize the MFR_mod variables within the those .mat files into one matrix. I feel like there is a smarter way then what I tried below. This will take me forever, since MFR_mod goes from 1 until 81.
for ii = 1:20
load(['silomodresults',num2str(ii),'.mat'])
end
MFR_tot = [MFR_mod1; MFR_mod2; MFR_mod3; MFR_mod4; MFR_mod5; MFR_mod6; MFR_mod7; MFR_mod8; MFR_mod9; MFR_mod10; MFR_mod11; MFR_mod12; MFR_mod13; MFR_mod14; MFR_mod15; MFR_mod16; MFR_mod17; MFR_mod18; MFR_mod19; MFR_mod20];
0 件のコメント
採用された回答
Stephen23
2020 年 10 月 19 日
編集済み: Stephen23
2020 年 10 月 19 日
D = 'path to the folder where the files are saved';
S = dir(fullfile(D,'silomodresults*.mat'));
C = {};
for k = 1:numel(S)
F = fullfile(D,S(k).name);
T = load(F,'-regexp','^MFR_mod\d+$');
N = fieldnames(T);
V = str2double(regexp(N,'\d+','once','match'));
C(V) = struct2cell(T); %#ok<SAGROW>
end
M = vertcat(C{:}); % optional
Giving:
>> size(M)
ans =
81 1
>> plot(M)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/385788/image.png)
9 件のコメント
Stephen23
2020 年 10 月 20 日
You could define all of the indices before the loop:
idc = {1,2,3,4,[5,11,20,38,81],[12,15,31,55,65],...};
and then inside the loop you can replace that entire huge lbock of code
if i== ..
..
elseif i== ..
..
..
end
with just this:
idx = idc{ii};
fnm = sprintf('silomodresults%d.mat',ii);
save(fnm,'MFR_mod','rho_bmod','idx')
It is still a mystery to me, how those indices are defined.
その他の回答 (2 件)
Ameer Hamza
2020 年 10 月 19 日
編集済み: Ameer Hamza
2020 年 10 月 19 日
One of the major problem is naming the variables like this x1, x2, ...: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. You can try something like this
MFR_tot = zeros(1, 20);
for ii = 1:20
data = load(['silomodresults',num2str(ii),'.mat']);
MFR_tot(ii) = data.(sprintf('MFR_mod%d', ii))
end
1 件のコメント
Mathieu NOE
2020 年 10 月 19 日
hello
seems all your mat file contains the same size of data
so the modification of you r code is fairly simple - if I understand what you want :
MFR_tot = [];
for ii = 1:20
data = load(['silomodresults',num2str(ii),'.mat']);
MFR_tot = [MFR_tot; data]; % concatenation
end
3 件のコメント
Mathieu NOE
2020 年 10 月 19 日
ok , now I understand
is there any possibility for you to generate dummy mat files , but much smaller. I'd like to try to solve it
I think the key thing is to extract the field (variables) names and then re order the whole thing based on the numeric content of the variable name
That shoudn't be too difficult for someone fluent in string / structure processing (I don't mean me only !)
I'll give it a try tomorrow if you can send me some dummy mat files in between
参考
カテゴリ
Help Center および File Exchange で Data Preprocessing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!