read a large number of hdf5 files with for loop and store in new variables
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ehsan Ben
am 5 Mai 2021
Bearbeitet: Stephen23
am 6 Mai 2021
Hello everyone,
I have about 1400 files in a folder in hdf5 format. A large number of data is stored in these files and I want to extract those numbers and and store in some variables I want. The name of these hdf5 files follows a specific order you can see in image below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/608395/image.png)
what I want is a "for loop" to choose each file, read it, and store its data to a new variable with a new index. To do so, I would like to do something like following, however, I know it doesn't work as "i" doesn't change in "ids", "fn", and "ft" names to generate a new variable with each loop. Please let me know what should I do. Thanks.
num_files = 700;
for i = 1 : num_files
ids_i = h5read('D:\University\Mcmaster University\Ph.D. Thesis\Data\3D Rigid Walls\Same Stress Path\Result\MATLAB\_triax_micro100kpa\1.h5','/interactions/ids');
fn_i = h5read('D:\University\Mcmaster University\Ph.D. Thesis\Data\3D Rigid Walls\Same Stress Path\Result\MATLAB\_triax_micro100kpa\1.h5','/interactions/normalForce');
ft_i = h5read('D:\University\Mcmaster University\Ph.D. Thesis\Data\3D Rigid Walls\Same Stress Path\Result\MATLAB\_triax_micro100kpa\1.h5','/interactions/shearForce');
end
2 Kommentare
Stephen23
am 5 Mai 2021
"Please let me know what should I do"
Use a cell array, just as the MATLAB documentation shows:
Do NOT generate new variables on each loop iteration, unless you really want to force yourself into writing slow, inefficient, complex, obfuscated, buggy code that is hard to debug. Indexing is neat, simple, and much more efficient.
Akzeptierte Antwort
Stephen23
am 6 Mai 2021
Bearbeitet: Stephen23
am 6 Mai 2021
"I want to make new variables as I have some post proccesing manipulation to do on these data."
You might want to do that, but using indexing is simpler, easier, more reliable, and more efficient. Using indexing makes looping over multiple files and storing their data very easy (unlike what you are trying to do). The MATLAB documentation recommends against creating new variables on the fly, as do all experienced MATLAB users.
Your code has several bugs and "features", e.g. you did not pass the generated filename as a variable (instead you wrote the variable name as part of some character vectors), you preallocate a cell array which you then ignore completely, and shadowing the inbuilt PATH function.
I recommend that you specify the path once and use FULLFILE instead of repeating the same hard-coded data over and over again (not a very nice code smell). This should get you started:
N = 648: % highest file number.
P = 'D:\University\Mcmaster University\Ph.D. Thesis\Data\3D Rigid Walls\Same Stress Path\Result\MATLAB\_triax_micro100kpa';
fmt_1 = h5info(fullfile(P,'1.h5'));
fmt_1bn = h5info(fullfile(P,'1_branch_normal.h5'));
ids = cell(N,2);
fn = cell(N,2);
ft = cell(N,2);
for k = 1:N
F = fullfile(P,sprintf('%d.h5',k));
ids{k,1} = h5read(F,'/interactions/ids');
fn{k,1} = h5read(F,'/interactions/normalForce');
ft{k,i} = h5read(F,'/interactions/shearForce');
F = fullfile(P,sprintf('%d_branch_normal.h5',k));
ids{k,2} = h5read(F,'/interactions/ids');
fn{k,2} = h5read(F,'/interactions/normalForce');
ft{k,2} = h5read(F,'/interactions/shearForce');
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu HDF5 finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!