Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

How to generate 2D arrays using the same variable appearing in a sequence of .mat files

1 view (last 30 days)
Dear All,
I have the following sequence of .mat files (540 files in total) saved in some directory
output_r10_tht00_imp30.mat
output_r10_tht00_imp35.mat
:
output_r10_tht00_imp70.mat
output_r10_tht15_imp30.mat
output_r10_tht15_imp35.mat
:
output_r10_tht15_imp70.mat
output_r10_tht30_imp30.mat
output_r10_tht30_imp35.mat
:
output_r10_tht30_imp70.mat
:
output_r10_tht135_imp70.mat
output_r11_tht00_imp30.mat
output_r11_tht00_imp35.mat
:
output_r11_tht00_imp70.mat
output_r11_tht15_imp30.mat
output_r11_tht15_imp35.mat
:
output_r11_tht15_imp70.mat
output_r11_tht30_imp30.mat
output_r11_tht30_imp35.mat
:
output_r11_tht30_imp70.mat
:
output_r15_tht135_imp70.mat
These are outputs of a Matlab simulation that was run for different combinations of values for the parameters ‘r’, ‘tht’ and ‘imp’ respectively. Each .mat file contains a 1x1 structure called ‘output’ that contains variables with the same names, e.g. A, B … etc. The numbers that follow the characters ‘r’, ‘tht’ and ‘imp’ in the above filenames denote the values for the respective parameters pertaining to each simulation. For example, the first one that appears in the list represents the combination r = 10, tht = 0, imp = 30, and so on. The parameter ‘r’ changes in increments of 1, the parameter ‘tht’ changes in increments of 15, and the parameter ‘imp’ changes in increments of 5. In other words, we could represent the parameter ‘r’ by the 1 x 6 array
r = [10 11 12 13 14 15];
the parameter ‘tht’ by the 1 x 10 array
tht = [0 15 30 45 60 75 90 105 120 135];
and the parameter ‘imp’ by the 1 x 9 array
imp = [30 35 40 45 50 55 60 65 70];
I would like to be able to concatenate the variable A (for example) from .mat files with imp = 30 into a 2D array called X (for example) whose rows are the length of ‘r’ and whose columns are the length of ‘tht’ (i.e. a 6 x 10 array). In this way, the element X(2,2) should be the value of the variable A contained in the file output_r10_tht15_imp30.mat, and so on. Furthermore, I would like to be able to repeat this process for imp = 35, imp = 40 etc.
Any ideas on how best to formulate a solution would be greatly appreciated.
Many thanks
Usman

Answers (1)

Matt J
Matt J on 30 Aug 2017
Edited: Matt J on 30 Aug 2017
S=dir('*imp30*.mat');
clear L
for i=numel(S):-1:1;
L(i)=load(S(i).name);
end
X=reshape([L.A],6,10);

This question is closed.

Community Treasure Hunt

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

Start Hunting!