How can I extract the numerical array from a hypercube object?

조회 수: 60 (최근 30일)
Miguel Ángel
Miguel Ángel 2026년 2월 3일 17:59
편집: dpb 대략 3시간 전
Hi! I've been using the hyperspectral library for the image processing toolbox for some years now. In the beggining, when a variable of class hypercube was created, it was storing the nomerical array (i.e. the spectral image) in a struct way that you could acces via dot indexing, for instance:
spectral_image = cube.DataCube;
However this seems not to be working anymore. Instead if I try this, I get a string instead of a numerical array. I've cheked and there is a funcion called gather which is supposed to be for this. I have also tried:
spectral_image = gather(cube);
But it is also not working. I get the following error, but my spectral image fits more than enough in my RAM, so I am sure this is not a problem.
Dot indexing is not supported for variables of this type.
Error in hypercube/gather (line 1398)
if isa(obj.BlockedDataCube.Adapter,'images.blocked.InMemory')
^^^^^^^^^^^^^^^^^^^^^^^^^^^
The problem is that I have spectral images stored in files in hypercube class that I saved some time ago with Matlab R2023 version or even earlier, but now I can not access directly to this numerical arrays that previously were perfectly fitting in my RAM.
Any clue about how could I fix this?
Thanks in advance.

채택된 답변

dpb
dpb 2026년 2월 3일 19:59
편집: dpb 2026년 2월 3일 20:23
The <Version History> section of the doc for R2025a discusses that the new version doesn't contain the actual datacube and that the prior versions will as you've discovered return the property as text. Why on earth that is is bizarre, indeed, it seems. But, it is what it is. Fortunately, they provide a helper function to convert to the expected numeric form. Why this wasn't included as a a builtin feature is anybody's guess.
From that doc page (sans my somewht cheeky side remarks) <vbg> --
"If you have saved hypercube objects created using the hypercube function in older releases prior to R2025a, the DataCube property contains the actual data cube. However, starting from R2025a, the DataCube property will be read as a string. You cannot use the new gather and apply object functions with these hypercube objects. To extract the actual data cube from hypercube objects created in prior releases, use this helper function."
function [datacube,wavelength,metadata] = classConverter(hcube)
datacube = str2double(hcube.DataCube);
metadata = hcube.Metadata;
datatype = metadata.DataType;
datacube = cast(datacube,datatype);
wavelength = hcube.Wavelength;
end
Good luck...
  댓글 수: 5
dpb
dpb 대략 2시간 전
str2double is notoriously slow -- with current MATLAB, you can convert directly and is quite a lot faster.
Try replacing
function [datacube,wavelength,metadata] = classConverter(hcube)
datacube = double(hcube.DataCube);
metadata = hcube.Metadata;
datatype = metadata.DataType;
datacube = cast(datacube,datatype);
wavelength = hcube.Wavelength;
end
Miguel Ángel
Miguel Ángel 대략 2시간 전
Yes. This way of conveting is indeed faster than using str2double. Yet not very fast, but at least I gain som time. I am not sure why Mathworks help recommended str2double which takes forever. I actually directly cast to single in one line and then build the new hypercube object using the new imhypercube function:
datacube = single(double(hcube.DataCube));
hcbue = imhypercube(datacube,hcube.Wavelength);
For some reason there's not a direct way of converting to single so instead you have to go through double. Yet the difference in computation time is not large.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Parth Parikh
Parth Parikh 대략 19시간 전
Hi Miguel,
I completely understand the frustration that comes with adapting to changes in well-established workflows. Moving forward, the best solution is to adopt the latest MATLAB functions for hyperspectral data, namely imhypercube and geohypercube. These provide seamless integration with current features, improved compatibility, and enhanced data management, positioning your work for future updates and optimizations from MATLAB. The helper function is meant to recover data from legacy hypercube objects created before R2025a, for robust, long-term workflows it’s highly recommended to transition to these newer functions. This way, you’ll be able to take advantage of MATLAB’s ongoing improvements with greater ease and reliability.
Note: For multispectral data, MATLAB provides the corresponding immuticube and geomulticube functions.
  댓글 수: 6
Miguel Ángel
Miguel Ángel 대략 5시간 전
Thanks @dpb.
Yes, the first approach you proposed is exactly how I will be working, We have some computer in our lab with older Matlab release so I guess it will work. Kind of time consuming but still faster than using concersion/casting from string to numbers.
Thanks a los for your help!
dpb
dpb 대략 4시간 전
편집: dpb 대략 3시간 전
Glad to try, anyway...good luck and let us know how it goes.
I still think this is well deserving a strong ping/complaint about unduly burdening the user with extra overhead cost.
ADDENDUM
Last thought...I think I'd request a working version of the old hcube object code that can be run with the new release, or conversely, a method that can use in the prior release to create the new objects as part of that complaint. It's only what they should have done to begin with.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Hyperspectral Image Processing에 대해 자세히 알아보기

제품


릴리스

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by