Appending variables to variable structure
3 次查看(过去 30 天)
显示 更早的评论
Hi Everyone,
I'm running into some trouble converting a set of dependent and independent loops into a function I can use to run over my entire dataset (one subject at a time).
I've created a function that takes the filename and group membership of each subject as inputs and I want to store the result of all the loops in one Results structure, to do so I use the following lines of code:
Results(1,1).Average(group,1) = OUTPUTA;
Results(1,1).Name = 'Average Output A';
Results(2,1).Average(group,1) = OUTPUTB;
Results(2,1).Name = 'Average Output B';
save('Results','Results');
Now the 'group' variable is one of the input and indicates group membership and thus the position of the results in resulting structure. This seems to work fine running, however, it overwrites my previous results every time I run the code. How do I run it such that when using:
function [Results] = Run_Analysis(filename, group)
%with group == 2
is doesn't overwrite the run of group == 1, but instead appends it to the existing Results.mat in the sense that the outcome for the next run is placed in the second row.
Cheers,
Richard
0 个评论
回答(2 个)
Jan
2012-4-11
I'm not sure, how the resulting struct array should look like. In general it should work like this:
if exist('Results.mat', 'file')
Results = load('Results.mat');
else
Results(1, 1).Name = 'Average Output A';
Results(2, 1).Name = 'Average Output B';
end
Results(1, 1).Average(group,1) = OUTPUTA;
Results(2, 1).Average(group,1) = OUTPUTB;
save('Results','Results');
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!