2

I'm trying to load about 40 files into MATLAB, all of who's names and locations are the same apart from 2 variable pieces. They each contain 5000 datapoints, which I want to combine into 1 single vector. They are HDF5 files, so I also have to specify the path inside the file, the name of which contains these variables too.

An example of loading one of these files would be

ROdata = double(h5read('directories\010340text2\010340text2.hdf5','/othertext2'));

Here, the variables would be 010340 and 2. So the general form is something like (n terms of x and y)

ROdata = double(h5read('directories\xtexty\xtexty.hdf5','/othertexty'));

Both x and y I have in an array already loaded into MATLAB.

From this point, I'm pretty clueless. I suppose num2str comes in somewhere, but I've tried a few things (like in the video http://blogs.mathworks.com/videos/2009/07/02/advanced-loading-files-using-their-names-for-variable-names/) but that doesn't work, as the variable part is inside the text, not at the end.

I could of course do so manually, but these 40 are from a series of around 300, so that'll be a lot of writing.

5
  • please include the pattern for how the two variables change (and their range). Commented Apr 8, 2014 at 14:22
  • There's no real pattern for x. It's 010307, 010323, 010340 for example, and even the numbers change now and then. They refer to a time. y is simply 0 to 24. Commented Apr 8, 2014 at 14:31
  • I think you should have a look at the dir function using wildcards: filenames = dir('directories\*text*\*text*.hdf5'); Commented Apr 8, 2014 at 14:33
  • 1
    Some general advice: First create all the strings, only then try to load them. This way it should be much easier to try stuff out. Commented Apr 8, 2014 at 14:33
  • @Dan I don't think the dir command can accept wildcards in folder names. Commented Apr 8, 2014 at 14:35

2 Answers 2

3

How about something like this:

ROdata = double(h5read( ... 
  sprintf( 'directories/%06dtext%d/%06dtext%d.hdf5', 010340, 2, 010340, 2 ), ...
  sprintf( '/othertext%d', 2 ) ));

%# or more general:
x = 010340;
y = 2;
ROdata = double(h5read( ...
  sprintf( 'directories/%06dtext%d/%06dtext%d.hdf5', x, y, x, y ), ...
  sprintf( '/othertext%d', y ) ));

%# or even more general:
%# assume X is your array containing elements x, Y for y elements

ROdata = cell{ numel(X), 1 };
for ii=1:numel(X)
  ROdata{ii} = double(h5read( ...
    sprintf( 'directories/%06dtext%d/%06dtext%d.hdf5', X(ii), Y(ii), X(ii), Y(ii) ), ...
    sprintf( '/othertext%d', Y(ii) ) ));
end

Have a look at the sprintf function, it's quite neat.

In order for this to work, you need to use forward slashes, as backslahes act as escape characters. Another possibility would be to use '\\', but '/' is more readable and works as well.

Since the numbers in X may contain leading zeros: %06d adds zero padding such that the inserted number will always be 6 digits long.

Sign up to request clarification or add additional context in comments.

6 Comments

Hm, I've tried a few times to make sure I wasn't messing up somewhere, but I'm getting Error using fclose Invalid file identifier. Use fopen to generate a valid file identifier. Error in h5read (line 58) fclose(fid);
Ok I found two issues: 1. You need to use forward slashes 2. You need to take care of the zero padding (for example in 010340). Are these values always of the same length?
Ah, you're right. I was about to post what went wrong, but the forward slashes in the directory name fix it. It still goes wrong after that, as you're right about the 0 padding. The values are always the same length, 6 numbers, but they don't always start with a 0. Most of them do though, so if it's hard to cater to both cases lets just stick to the ones starting with a 0!
Cool! No, you can actually solve this nicely, let me find the correct padding function, I'll update the answer accordingly!
%06d adds zero padding such that the inserted number will always be 6 digits long.
|
1

What about using string concatenation?

ROdata = double(h5read(['directories\xtexty\' num2str(x) 'xtexty.hdf5'],['/othertexty' num2str(y)]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.