Appending variables to variable structure

조회 수: 4 (최근 30일)
Richard
Richard 2012년 4월 11일
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

답변 (2개)

Jan
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');

Richard
Richard 2012년 4월 11일
Hi Jan,
Thanks for the quick answer, unfortunately it doesn't quite do the trick. This just adds another Results structure within the original results each time I run it (so I kinda expand the structure with the same structure everytim I run it).
What I want the final results to look like is one structure that contains lists of variable and every variable list should contain the variable of all subjects (so that every time I run/call the function) with its 'listindex' or group membership that person outcome measures are added to all the lists in the structure.
Best wishes,
Richard

카테고리

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