Loading a .mat file as a cell and without changing its size

조회 수: 16 (최근 30일)
SelDen
SelDen 2022년 3월 11일
편집: Stephen23 2022년 3월 11일
I have a large cell array in a .mat file which I want to load into the code. But when I write
data = load('temp.dat');
data is a 1x1 struct. How can I save the data as a cell array (not as 1x1 cell) ?
  댓글 수: 1
Stephen23
Stephen23 2022년 3월 11일
편집: Stephen23 2022년 3월 11일
The robust approach:
S = load('temp.dat');
data = S.nameOfFieldInMatFile
And in the special case that there is only one variable in the file and you do not know its name:
C = struct2cell(load('temp.dat'));
data = C{1};
If you are interested in writing robust code it is best to avoid LOADing directly into the workspace.

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

답변 (2개)

Sugar Daddy
Sugar Daddy 2022년 3월 11일
Look son, here is an easy way
load('temp.dat');
just load it, no need to assign it to specific variable

Star Strider
Star Strider 2022년 3월 11일
It depends on what is in ‘temp.dat’.
If it only contains the cell array, just load it into your workspace. If it contains other vairables that are not desired, specify the variable to load:
load('data.dat','myCellArray')
It will then exist in your workspace with that variable name.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by