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

73 vues (au cours des 30 derniers jours)
Miguel Ángel
Miguel Ángel le 3 Fév 2026 à 17:59
Modifié(e) : dpb le 6 Fév 2026 à 19:08
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.

Réponse acceptée

dpb
dpb le 3 Fév 2026 à 19:59
Modifié(e) : dpb le 3 Fév 2026 à 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...
  6 commentaires
Miguel Ángel
Miguel Ángel le 5 Fév 2026 à 10:42
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.
dpb
dpb il y a environ 9 heures
Modifié(e) : dpb il y a environ 6 heures
Once having done the work to scan the text and do the conversion to internal storage form, the cast to single() will be quick, yes. MATLAB doesn't natively support any alternatives to skip the conversion of floating point representations to double. Even in C, using the '%f' conversion format to a &float memory addresss, it's likely the io runtime code actually does the conversion to double internally and then the cast is done on storage.

Connectez-vous pour commenter.

Plus de réponses (1)

Parth Parikh
Parth Parikh le 5 Fév 2026 à 4:17
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 commentaires
Miguel Ángel
Miguel Ángel le 5 Fév 2026 à 18:27
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 le 5 Fév 2026 à 19:14
Modifié(e) : dpb le 5 Fév 2026 à 20:30
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Hyperspectral Image Processing dans Help Center et File Exchange

Produits


Version

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by