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

57 ビュー (過去 30 日間)
Miguel Ángel
Miguel Ángel 2026 年 2 月 3 日 17:59
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 約8時間 前
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 約1時間 前
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 約15時間 前
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 約1時間 前
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分 前
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeHyperspectral Image Processing についてさらに検索

製品


リリース

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by