How to selectively save the names and values of some workspace variables to a CSV file?
3 次查看(过去 30 天)
显示 更早的评论
Consider the following
a = 3; b=7; c=5; d=9;
save('test.mat')
data = load('test.mat');
toSave = { 'b' ; 'c' };
writecell( horzcat( toSave , <missing code> ) ,'myfile.csv')
what should be the <missing code> in order to have the content of myfile.csv to be
b , 7
c , 5
?
采纳的回答
Rik
2020-9-15
This should do the trick. If the variables aren't scalars the results might not be what you want.
a = 3; b=7; c=5; d=9;
save('test.mat')
data = load('test.mat');
toSave = { 'b' ; 'c' };
s=struct;
for n=1:numel(toSave)
s.(toSave{n})=data.(toSave{n});
end
writecell( horzcat( toSave , struct2cell(s) ) ,'myfile.csv')
3 个评论
Rik
2020-9-15
You could also ask load to only retrieve the variables of interest:
s=load('test.mat',toSave{:});
Note that loops have a pretty good performance, as long as you aren't dynamically growing arrays and there isn't a direct function to do it.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!