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

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Bob Thompson
Bob Thompson am 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 Kommentare
Kathryn Baker
Kathryn Baker am 18 Jun. 2019
Bearbeitet: Kathryn Baker am 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 am 18 Jun. 2019
Bearbeitet: Bob Thompson am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by