h5 KeyError: "Unable to open object (object 'data' doesn't exist)" - h5py

I am taking a previously generated .h5 file which is 3D and taking a 2D slice. The new 2D h5 file looks complete and as expected, however I then pass this into some modeling software (GPRMax) and get the error KeyError: "Unable to open object (object 'data' doesn't exist)". Previous 2D and 3D .h5 files have been fine, so im not sure whats different. Below is the code used to generate the .h5 file. Any help is appreciated.
hf = h5py.File(dir_path+'\ModelMC0105\ModelA3d1_15102020_170900005701.h5', 'r')
dset = hf['data']
if os.path.exists(dir_path+'\ModelMC0105\ModelA3d1_15102020_170900005701-slice.h5'):
os.remove(dir_path+'\ModelMC0105\ModelA3d1_15102020_170900005701-slice.h5')
hf2 = h5py.File(dir_path+'\ModelMC0105\ModelA3d1_15102020_170900005701-slice.h5','w')
dz_dy_dz = (0.001,0.001,0.001)
hf2.attrs['dx_dy_dz'] = dz_dy_dz
data = hf2.create_dataset("data", (100,1,100))
for i in range(100):
data[i]=(dset[i,50,:])
data=data.ref
print(data)
hf2.close()
hf.close()

Try absolute path, please. It may works.

Here is an example that creates a ['data'] dataset in an HDF5 file, closes, then reopens (read-only) and copies the data in slices to a second HDF5 file. Maybe you will find something to help you.
with h5py.File('SO_64758047.h5','w') as h5w:
arr = np.random.random(100*480*640).reshape(100,480,640)
ds = h5w.create_dataset('data', data=arr)
with h5py.File('SO_64758047.h5','r') as h5r:
dset = h5r['data']
with h5py.File('SO_64758047_2.h5','w') as h5w2:
dz_dy_dz = (0.001,0.001,0.001)
h5w2.attrs['dx_dy_dz'] = dz_dy_dz
data = h5w2.create_dataset('data',(100,480,640))
for i in range(h5r['data'].shape[0]):
print('get slice#',str(i))
data[i] = h5r['data'][i,:,:]

Related

How to automatically change variable names given a new input file name

I am importing data from a .mat file and then extracting certain signals from it and I call this data, data. data is a 1x1 struct with 1 field, FT_est_X, where X is the the particular run that I collected the samples from. Here is the code snippet of how I do that.
data = load('site_data_all_2.mat');
t = data.FT_est_2.time;
% estimated data
Fx = data.FT_est_2.signals(1).values;
Fy = data.FT_est_2.signals(2).values;
Fz = data.FT_est_2.signals(3).values;
Mx = data.FT_est_2.signals(4).values;
My = data.FT_est_2.signals(5).values;
Mz = data.FT_est_2.signals(6).values;
So, you can see that this data was collected from run 2. Now, let's say I want to load in a file named site_data_all_3.mat (run 3), what happens is that all the data below %estimated data changes its name--everything stays the same, except the 2 becomes a 3 (e.g. Fx would be Fx = data.FT_est_3.signals(1).values;. Currently, I have to manually enter in the 3 for each variable; can anyone tell me how I can only change the file name and it will automatically change the variable names for me? Essentially, I just want it to be Fx = data.name_of_struct_field.signals(1).values.
Thank you!
You could construct the string some programmatic way (maybe with an iteration variable), but here's the simple answer of defining the fieldname as a string and simply using it. At the next iteration, update the fieldname variable and repeat.
fieldname = 'FT_est_2';
Fx = data.(fieldname).signals(1).values;

how could I read multiple series in one xlsx file and save in .mat?

First sorry for basic questions..
What I've done was importing ".csv" to matlab using "readtable. But I don't have idea as to making this 367 different time series to be independent .mat files.
result = readtable('price.csv');
result is shown in image file to help understanding..
How could I make this process automatically?? Please help me..
result = readtable('price.csv');
for i = 1:2:734
M = rmmissing(result(:,[i,i+1]));
str = M.Properties.VariableNames(2);
% str{1}(regexp(str{1}, 'x')) = [];
name = str{1};
save(['C:\Users\bok\Desktop\mat\',name],'M');
end

for loop+structure allocation in matlab

This is a problem I am working on in Matlab.
I am looping through a series of *.ALL files and stripping the field name by a delimiter '.'. The first field is the network, second station name, third location code and fourth component. I pre-allocate my structure based on the number of files (3) I run through which for this example is a 3x3x3 structure that I would like to define as struct(station,loc code,component). You can see these definitions in my code example below.
I would like to loop through the station, loc code, and component and fill their values in the structure. The only problem is for some reason the way I've defined the loop it's actually looping through the files more than once. I only want to loop through each file once and extract the station, comp, and loc code from it and put it inside the structure. Because it's looping through the files more than once it's taken like 10 minutes to fill the structure. This is not very efficient at all. Can someone point out the culprit line for me or tell me what I'm doing incorrectly?
Here's my code below:
close all;
clear;
[files]=dir('*.ALL');
for i = 1:length(files)
fields=textscan(files(i).name, '%s', 'Delimiter', '.');
net{i,1}=fields{1}{:};
sta{i,1}=fields{1}{2};
loc{i,1}=fields{1}{3};
comp{i,1}=fields{1}{4};
data = [];
halfhour(1:2) = struct('data',data);
hour(1:24) = struct('halfhour',halfhour);
day(1:366) = struct('hour',hour);
PSD_YE_DBT(1:length(files),1:length(files),1:length(files)) =
struct('sta','','loc','','comp','','allData',[],'day',day);
end
for s=1:1:length(sta)
for l=1:1:length(loc)
for c=1:1:length(comp)
tempFileName = strcat(net{s},'.',sta{s},'.',loc{l},'.',comp{c},'.','ALL');
fid = fopen(tempFileName);
PSD_YE_DBT(s,l,c).sta = sta{s};
PSD_YE_DBT(s,l,c).loc = loc{l};
PSD_YE_DBT(s,l,c).comp = comp{c};
end
end
end
Example file names for the three files I'm working with are:
XO.GRUT.--.HHE.ALL
XO.GRUT.--.HHN.ALL
XO.GRUT.--.HHZ.ALL
Thanks in advance!

MATLAB Error using importdata

I get an error at the importdata line saying
"Error using importdata (line 137)
Unable to open file."
when I write in 'specimenAl.dat' manually into the function, the program runs fine. How can I make use of the importarray function while defining the argument as an element of an array?
material = {('specimenAl.dat'), ('specimenSt.dat')};
A = importdata(material(1));
Data = A.data;
Force = Data (:,2);
Displacement = Data (:,1);
Strain = Data (:,3);
I had the same problem. fixed it by using {} in
A = importdata(material{1});
Hope this helps!

How to read a lot of DICOM files with Matlab?

I am using a script which generate a collection of strings in a loop:
'folder1/im1'
'folder1/im2'
...
'folder1/im3'
I assign the string to a variable, when I try to execute the img = dicomread(file); function I get the following error:
Error using dicomread>newDicomread (line 164)
The first input argument must be a filename or DICOM info struct.
Error in dicomread (line 80)
[X, map, alpha, overlays] = newDicomread(msgname, frames);
Error in time (line 14)
img = dicomread(file);
However, using the command line I don't get errors: img = dicomread('folder1/im1').
The code is the next:
for i=1:6 %six cases
nameDir = strcat('folder', int2str(i));
dirData = dir(nameDir);
dirIndex = [dirData.isdir];
fileList = {dirData(~dirIndex).name}; % list of files for each directory
n = size(fileList);
cd(nameDir);
for x = 1:n(2)
img = dicomread(strcat(pwd(), '/', fileList(x)));
end
cd('..');
end
What could be the error?
You've figured it out by now, haven't you.
Based on what you've written, you test
img = dicomread('folder1/im1');
when what you are having trouble with is
img = dicomread(file);
You need to actually test the line you are having trouble with. I would recommend:
putting a break point in test.m a the line img = dicomread(file). When you get to that line you can see what file is equal to. Also do whos file to make sure it is of class char and not a cell array or something random.
If you still want help, edit your original post and show the code where you assign those strings to file and tell us what happens when you type img = dicomread(file) at the command prompt.