save problem: -STRUCT must be the name of a scalar structure variable
Show older comments
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 Comment
Stephen23
on 23 Jul 2021
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');
Accepted Answer
More Answers (1)
埃博拉酱
on 23 Jul 2021
1 vote
save(filename,'-fromStruct',test);
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!