How to save output results for loop iterations

7 views (last 30 days)
Hello, I am trying to make a loop that will iterate based on xlsx table and I want to save the result from each itetarion in a table (workspace).
tbl = readtable('Table.xlsx');
Hs = table2array(tbl (:,8));
Ws = table2array(tbl (:,6));
Mwd = table2array(tbl (:,9));
Tp = table2array(tbl (:,10));
%Start=0,Step=30,End=360
s=30;
for x=0:s:360
MWDD = table2array(varfun(@(a)((a>=x) & (a<(x+s))), tbl(:,9)));
MWD = tbl(MWDD,:);
end
Thank you for any help.

Answers (1)

Steven Lord
Steven Lord on 8 Mar 2023
Rather than converting from a table to an array to perform your operations, I think what you want to do is to discretize the 9th variable in your table to determine into which bin of data each element falls. Once you have those bin/group numbers if you want to operate on each group in turn use groupsummary, grouptransform, or some other grouping function.
Oh, I just noticed that this is tagged with release R2017b. In that case neither groupsummary nor grouptransform are available (introduced in releases R2018a and R2018b respectively.) In that case look at splitapply. I'd still use discretize but that's because I'm more familiar with it; there is also a more general findgroups function that you could use if you may need to generalize to non-numeric and non-categorical data in the future.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!