Filter löschen
Filter löschen

Loading part of a structure in a .mat file

15 Ansichten (letzte 30 Tage)
user86753
user86753 am 27 Nov. 2019
Bearbeitet: Stephen23 am 27 Nov. 2019
Is there a way to load part of a cell array that has been saved to a .mat file?
I have a cell array as outlined below and saved to disk as 'test.mat'
test{1}
test{2}
test{3}
Can I load only test{1} from the saved .mat file?
I have tried the code below but I get the following error
a = matfile('test.mat');
dat = a.test{1};
Cannot index into 'test' because MatFile objects only support '()' indexing.

Akzeptierte Antwort

Stephen23
Stephen23 am 27 Nov. 2019
Bearbeitet: Stephen23 am 27 Nov. 2019
The matfile documentation states "The MAT-file object does not support indexing into... Cells of cell arrays..."
This means you can use parenthesis indexing to return the cells themselves, but not curly-brace indexing to access the contents of the cells:
"Is there a way to load part of a cell array that has been saved to a .mat file?"
Of course, you can use parentheses (which will return a cell array, from which you can trivially access its contents):
>> C = {'hello','world',NaN};
>> save('test.mat','C','-v7.3');
>> clear
>> M = matfile('test.mat');
>> D = M.C(1,2) % D is a cell array containing a character vector
D =
'world'
>> S = D{1} % S is a character vector
S =
world

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by