save problem: -STRUCT must be the name of a scalar structure variable

조회 수: 29 (최근 30일)
Yu Li
Yu Li 2018년 9월 14일
댓글: Stephen23 2021년 7월 23일
I generated a struct file and tried to save it, but Matlab occurs error: The argument to -STRUCT must be the name of a scalar structure variable.
Below are the test code:
for i=1:1:100
fprintf('%0dth data.\n',i)
eval(['test.p',num2str(i),'=1:1:2e5;'])
end
save('test.mat','-v7.3','-struct','S');
is there any problem with my code?
Thanks!
Yu
  댓글 수: 1
Stephen23
Stephen23 2021년 7월 23일
Rather than using evil EVAL, it is simpler, more efficient, and much more robust to use dynamic fieldnames:
test = struct();
for k = 1:100
fld = sprintf('p%d',k);
test.(fld) = 1:1:2e5;
end
save('test.mat','-v7.3','-struct','test');

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

채택된 답변

Stephen
Stephen 2018년 9월 14일
It is unclear from your post what the data type of variable "S" is. But the error is telling you that "S" is NOT a structure. You can either:
1) Change "S" to a structure variable containing the data you want to save.
or
2) Remove the '-struct' argument from the save command.

추가 답변 (1개)

埃博拉酱
埃博拉酱 2021년 7월 23일
save(filename,'-fromStruct',test);

카테고리

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