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

조회 수: 2 (최근 30일)
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

채택된 답변

Bob Thompson
Bob Thompson 2019년 6월 18일
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
Kathryn Baker
Kathryn Baker 2019년 6월 18일
편집: Kathryn Baker 2019년 6월 21일
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 2019년 6월 18일
편집: Bob Thompson 2019년 6월 18일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by