MATLAB Answers

How to pull the data value from a 1x1 struct with 12 fields and store that for corresponding images in a pixelLabelDatastore?

17 views (last 30 days)
Xavier Oberhelman
Xavier Oberhelman on 6 Nov 2020
Commented: Raunak Gupta on 31 Dec 2020 at 3:31
Hey i'm trying to read in volumes to ultimately train a network but im encountering this error:
Error using trainNetwork (line 183)
The output of the custom ReadFcn must be a categorical matrix.
Error in praxisNetwork1_0 (line 40)
net = trainNetwork(ds,lgraph,options)
Caused by:
Error using (line 309)
The output of the custom ReadFcn must be a categorical matrix.
I'm reading in .nrrd files for the pixelLabelDatastore and manage to read them in with the mathworks .nrrd reader however I can't seem to get just the data field of the struct that gets read in. I believe the categorical matrix error is occurring because the network attempts to utilize this struct field instead of the categorical matric that is the data field. This issue doesnt appear when I read in the training images because those files came as .mha's which are read in as categorical matrices. Any ideas or suggestions are appreciated.


Sign in to comment.

Accepted Answer

Raunak Gupta
Raunak Gupta on 13 Nov 2020
From seeing all the code files attached in the code, I see that nha_read_volume returns a volume which can be used as training image whereas nhdr_nrrd_read returns a structure which contains many fields and information about the whole file. Since the ReadFcn should return a categorical array to use in pixelLabelDatastore, I will suggest creating a separate ReadFcn which uses nhdr_nrrd_read to read the strcuture from raw file and then select the data field out of the structure. Also make sure to convert the data into categorical array as I mentioned in a similar question and the structure contains the data field.
An example for that ReadFcn
function data = readNRRD(filename)
nrrdHeader = nhdr_nrrd_read(filename,true);
data = categorical(;


Xavier Oberhelman
Xavier Oberhelman on 30 Dec 2020 at 23:13
Hey Raunak Gupta,
Thanks for answering this and your solution solved the issue. I'm currently attempting to use the labels of 'background' and 'kidney' with my network and use it to segment based off that. However I encounter an error:
It makes sense that the files being pulled are lacking the labels I desire however, would I just declare the labels in the fuction readNRRD as a valueset, pictured below, and if so how do I know these two labels are correctly being applied to the areas of my selected label volume.
Finally if i'm not too declare them, and they correlate to one of the fields of my struct file before pulling the data out: Which is the one it correlates to? Image of struct fields provided below.
Thanks again for your help and any advice & knowledge is appreciated. Have a great day!
Raunak Gupta
Raunak Gupta on 31 Dec 2020 at 3:31
Hi Xavier,
I understand that a label file with exact categories like "background" and "kidney" is not present but I assume for supervised classification a label file will be there. I do think that the label file will be seperate from the data file. If the labels are there and have the same format it can be read with the same method. Generally if its a label file then it will in binary format containing 0s and 1s. You can replace the 0s and 1s with particular string labels and that will make it suitable for passing into pixelLabelDatastore.
Syntax for replacing:
labelsCat = categorical(labelsBinary,[0,1],{'background','kidney'});
Here the "background" will be assigned in place of 0s and "kidney" will be assigned to 1s.
If there are more labels the vector and cell array can be extended.
As for the last question I think the categories present in data must be encoded into "kinds" field of the struct but I am not sure since I haven't worked with this data.
Hope this helps!

Sign in to comment.

More Answers (0)





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!