| 1 | % hdf5save_recursion.m |
|---|
| 2 | % |
|---|
| 3 | % hdf5save('filename','prefix',new,'name in workspace','name in hdf5 file',...) |
|---|
| 4 | |
|---|
| 5 | % Save the given variables in an hdf5 file in the groupe given by the |
|---|
| 6 | % prefix. If new is set to one creates a new hdf5 file. Syntax Similar to |
|---|
| 7 | % the native matlab save command. Can save strings, arrays, and structs. |
|---|
| 8 | % If it find a struct it calls its recusively to save the struct, passing |
|---|
| 9 | % the struct name as the prefix. |
|---|
| 10 | |
|---|
| 11 | % Author: Gael Varoquaux <gael.varoquaux@normalesup.org> |
|---|
| 12 | % Copyright: Gael Varoquaux |
|---|
| 13 | % License: BSD-like |
|---|
| 14 | |
|---|
| 15 | function hdf5save_recursion(filename,prefix,new,varcell) |
|---|
| 16 | |
|---|
| 17 | if ~strcmp(class(filename),'char'); |
|---|
| 18 | error('first argument should be a string giving the path to the hdf5 file to save'); |
|---|
| 19 | end |
|---|
| 20 | |
|---|
| 21 | if ~strcmp(class(prefix),'char'); |
|---|
| 22 | error('second argument should be a string giving the name of the groupe to save the data in'); |
|---|
| 23 | end |
|---|
| 24 | |
|---|
| 25 | if new~=1 && new~=0; |
|---|
| 26 | error('third argument should be 0 or 1: 0 to append data, and 1 to create a new file'); |
|---|
| 27 | end |
|---|
| 28 | |
|---|
| 29 | nvars=size(varcell,2)/2; |
|---|
| 30 | |
|---|
| 31 | if nvars~=floor(nvars); |
|---|
| 32 | error('expecting a name for each variable'); |
|---|
| 33 | end |
|---|
| 34 | |
|---|
| 35 | for i=[1:nvars] |
|---|
| 36 | str=varcell{2*i}; |
|---|
| 37 | var=evalin('base',str); |
|---|
| 38 | name=varcell{2*i-1}; |
|---|
| 39 | type=class(var); |
|---|
| 40 | location=strcat('/',prefix,name); |
|---|
| 41 | %disp(['variable name in workspace : ',str]); |
|---|
| 42 | %disp(['variable name to put : ',name]); |
|---|
| 43 | switch type |
|---|
| 44 | case 'struct' |
|---|
| 45 | names=fieldnames(var); |
|---|
| 46 | for j=1:size(names,1); |
|---|
| 47 | if (j~=1 || i~=1) |
|---|
| 48 | new=0; |
|---|
| 49 | end |
|---|
| 50 | varname=strcat(name,'.',names{j}); |
|---|
| 51 | hdf5save_recursion(filename,strcat(name,'/'),new,{names{j},varname}); |
|---|
| 52 | end |
|---|
| 53 | otherwise |
|---|
| 54 | location=strcat('/',prefix,name); |
|---|
| 55 | if new==1 && i==1; |
|---|
| 56 | hdf5write(filename,location,var); |
|---|
| 57 | else |
|---|
| 58 | %disp(['location : ',location]); |
|---|
| 59 | hdf5write(filename,location,var,'WriteMode', 'append'); |
|---|
| 60 | end |
|---|
| 61 | end |
|---|
| 62 | end |
|---|
| 63 | end |
|---|