Generating multiple max values from various data sets

Hello,
At the moment my code reads through 37 data sets and grabs the max of each spradsheet and outputs the max pressure readings from each of the sheets for 31 points. So at the end I have 31 columns and 37 rows.
I am trying to alter the code so it would give me an output of the top 10 peaks of each instead of 1 for each point. That way I am able to take an average of it all.
I am struggling to sort the values and make it output the 10 for each file. I can't use maxk because I am using an older version.
This is my code at the moment.
Temp = xlsread('C;\Temperature Data.xlsx','B:B');
angle = 0:10:360;
nports = 64;% including the two ports for the reference Pitot static tube
Uinf = zeros(length(angle),1);
Max = zeros(length(angle),(nports-2)/2);
for ii = 1:length(angle)
load(['20200401_Existing_',num2str(angle(ii)),'.mat']); % loading the mat file -
% Note: if you are using windows machine, use backward slash in the folder address
rho = 101325./((Temp(ii) + 273.15)*286.9); % ideal gas equation
Uinf(ii) = sqrt(mean(2*abs(data{:,2}-data{:,3}))./rho); % free stream velocity
press = sqrt(2*abs(data{:,4:2:end}-data{:,5:2:end})./rho)./Uinf(ii);
Max(ii,:) = max(press,[],1); % Maximum of pressure fluctuations
end
xlswrite('Test_Max',Max);
Thank you,

 Akzeptierte Antwort

Mohammad Sami
Mohammad Sami am 29 Apr. 2020
Bearbeitet: Mohammad Sami am 29 Apr. 2020
You can use the sort function to sort the variable in descending order.
I changed the max to a cell array, so that you can store the top 10 rows.
If you are comfortable, you can also use a 3 dimensional matrix.
Max = cell(length(angle),1);
%....
%....
press_sorted = sort(press,'descend');
Max{ii} = press_sorted(1:10,:);
%....

7 Kommentare

And if even one release later were being used, R2017b instead of R2017a, then you could use mink() or maxk()
Hi, thanks for the reply I had adjusted the code accordingly and the output file I am getting still gives me one max of each data set that I am loading in.
Instead of having 37 rows consisting of the maximum pressure (for each point) I would like to have 370 rows (10 from each to be in descending order).
Thank you for your help so far!
angle = 0:10:360;
nports = 64;% including the two ports for the reference Pitot static tube
Uinf = zeros(length(angle),1);
Max = cell(length(angle),1);
for ii = 1:length(angle)
load(['20200401_Existing_',num2str(angle(ii)),'.mat']); % loading the mat file -
% Note: if you are using windows machine, use backward slash in the folder address
rho = 101325./((Temp(ii) + 273.15)*286.9); % ideal gas equation
Uinf(ii) = sqrt(mean(2*abs(data{:,2}-data{:,3}))./rho); % free stream velocity
press = sqrt(2*abs(data{:,4:2:end}-data{:,5:2:end})./rho)./Uinf(ii);
press_sorted = sort(press,'descend');
Max{ii} = press_sorted(1:10,:);
end
xlswrite('Test_Max',Max);
that should be correct. we add in one more line to concatenate all the max values together
% Max{ii} = press_sorted(1:10,:); % this will select top 10 rows
% end
Max = vertcat(Max{:}); % concatenate
xlswrite('Test_Max',Max);
Thanks for the reply.
I just tried to add the line of code in for the concatenate and i got, "Cell contents reference from a non-cell array object" for the line we had just added into the code.
When I run the previous code you had mentioned:
Max = cell(length(angle),1);
%....
%....
press_sorted = sort(press,'descend');
Max{ii} = press_sorted(1:10,:);
%....
It seems like the output file has tried to copy values into the spreadsheet but there are no values present. (boxes are highlighted but no values are visible in cells).
I apologise for the previous post as well,I had not deleted my old values in the spreadsheet when I ran the code.
I am not clear as to whether you currently have the line
Max = vertcat(Max{:}); % concatenate
in your code? After that line, Max will no longer be a cell array and you would not be able to
Max{ii} = press_sorted(1:10,:);
All sorted. Thank you guys much appreciated!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by