How can I put the output of each iteration into one table to find averages of each row later?

2 views (last 30 days)
Here is the code I have used to loop through and process all files within a directory. It produces a graph of all of the iterations, but outputs each iteration as a separate command in the command window. Instead, I would like to add the results of each iteration into a table so that I can collate the data and find the average of each row later on. I have tried using zeros() but this distorts the data in the graph so it only shows one iteration output.
%%import folder location
path='file path';
fil=fullfile(path,'area*.txt');
d=dir(fil);
for k=1:numel(d)
filename=fullfile(path,d(k).name);
delim=';';
headings=6;
A=importdata(filename, delim, headings);
data=A.data;
variable1=data(:,1);
variable2=data(:,2);
%find minimum and maximum
%minimum
%isolate bottom 0.5 percentile
minimum=prctile(variable1, 0.5);
%maximum
maximum=prctile(variable1, 99.5);
%normalise data
normalised(k)=(variable1(k)-minimum)/(maximum-minimum);
%smooth graph
a=linspace(0,1,100);
b=spline(normalised, variable2, x);
%plot distribution figure 1
figure(1)
plot(a,b,'.-');
hold on
end
hold off

Accepted Answer

Bob Thompson
Bob Thompson on 18 Jun 2019
The simplest way to do this is to index b.
b(:,k) = spline(normalised_IQ, number_count, x);
% and then down in the plot section
plot(a,b(:,k),'.-')
I think that should take care of what you are looking for with regards to the storage.
Taking the averages of each column should be done outside the loop. Just add the following after the loop.
aves = mean(b,1);
  2 Comments
Kathryn Baker
Kathryn Baker on 18 Jun 2019
Edited: Kathryn Baker on 21 Jun 2019
Thank you for your quick response!
How would this work if I was not using the spline feature? As I would like to produce a table of 12 columns and 20 rows, with each iteration (6 in total) filling two columns, the first with variable 2 and the second with variable1, rather than the spline information. I am only using the spline to smooth the graph.
Bob Thompson
Bob Thompson on 18 Jun 2019
Edited: Bob Thompson on 18 Jun 2019
The key part of what I did is on the left side of the equal sign, so the basic concept is the same. If you are looking to put two columns in at once then I would expect it to look something like the following:
% Single command format
b(:,2*k-1:2*k) = [number_count, normalised_IQ];
% Double command format; Choose only the above single or the below double
b(:,2*k-1) = number_count;
b(:,2*k) = normalized_IQ;
This may not be exactly what you're looking for, but it should be a rough idea.
For a more detailed description of indexing, check out this.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!