MATLAB Answers

How to iterate and import nested data in a h5 file

3 views (last 30 days)
Dallan Friel
Dallan Friel on 10 Dec 2019
Edited: Dallan Friel on 11 Dec 2019
I'm trying to write a piece of code which will import several vectors that contain numbers from a hdf5 file and then create an array of the vectors.
The difference between the path of each vector in the h5 file is one number in the path
example: /case1/... /case2/.... /case3... and so on.
At the moment the process is quite laborous and I'd like to possibly use something like a 'for' loop to import each vector and store them in an array:
Example:
arrayname = (vector1, vector2, vector3, vector4);
This is how I read one vector within the file:
So I would specify the directory of the file then the path of each vector,
vector1 = h5read('C:\Users\...', '/example/example/case1');
I was thinking of using something like this:
number_of_cases = 20;
number_of_time_steps = 3000; % This is the length of the vectors
m = zeros(number_of_cases,number_of_time_steps); % Populating an array 'm' with zeros to the exact size it will be when the numbers are imported into it
for i = 1:number_of_cases
m(i:) = h5read('C:\Users\....', '/path/case(i)');
end
I'm not quite sure what I need to do? Is it possible to iterate when the 'i' variable is contained in the path? The 'for' loop probably needs some work also.
Any help much appreciated!

  0 Comments

Sign in to comment.

Accepted Answer

Dallan Friel
Dallan Friel on 11 Dec 2019
Edited: Dallan Friel on 11 Dec 2019
I got it to work using this!
t =[];
for i = 1:22% there are 22 vector
j = int2str(i);
p = '/Response_Spec_Workflow_Main/Response_Spec_Workflow_Main_'; % This is the preceding part of the path that comes before the number I wish to iterate over.
k = '/FPV_Tri_Float_BM_10x/Initial/Dynamic/Floater_2/segment_1/node_4/Displacement in x - direction'; % Proceeding portion of path the path that comes after the number
s = strcat(p,j,k); %
r = h5read('C:\Users\..',s); % the file directory and s contains the path
t =[t,r,];
end
Because the number I wish to change for every iteration is in the middle of the path I have converted the number to a string then concatenated the first part of the path (string), then the number, then part which comes after (string),

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.