Sorting maximum value array by same date and time
Mostra commenti meno recenti
I have an array of very large size 708273*3,the first and second column representing time and date respectively. Each time and date have many values. I need to find out maximum value from that. I have no idea with how to short those value from excel. Sample data file name "test1".
Any help will be highly appreciated.
With best regards,
Somnath
Risposte (1)
Ameer Hamza
il 28 Mag 2020
Try this
T = readtable('test1.xlsx');
grps = findgroups(T.Time, T.Date);
max_vals = splitapply(@max, T.Value, grps);
T.MaximumValue = max_vals(grps);
writetable(T(:,'MaximumValue'), 'test1.xlsx', 'Range', 'D1')
24 Commenti
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
What is the error?
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
When I run this code, I get following values in the excel file

SOMNATH MAHATO
il 28 Mag 2020
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
In MATLAB, the file name must not contain spaces. Remove the spaces from the name of the script and the use green button in the toolbar to run the code.
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
You haven't changed the name of your script. Change it from "maximum elevaltion angle short.m" to something else, and it should not have spaces in its name.
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
Which MATLAB version are you using?
SOMNATH MAHATO
il 28 Mag 2020
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
Try this
T = readtable('test2.xlsx');
temp = num2str(T.Time, 16);
temp = mat2cell(temp, ones(size(temp,1),1), size(temp,2));
[~, ~, grps] = unique(strcat(temp, T.Date));
max_vals = splitapply(@max, T.Value, grps);
T.MaximumValue = max_vals(grps);
writetable(T(:,'MaximumValue'), 'test2.xlsx', 'Range', 'D1')
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
Try this
T = readtable('test2.xlsx');
temp = num2str(T.Time, 16);
temp = mat2cell(temp, ones(size(temp,1),1), size(temp,2));
[~, ~, grps] = unique(strcat(temp, T.Date));
u_grps = unique(grps);
max_vals = zeros(numel(u_grps), 1);
for i=1:numel(u_grps)
max_vals(i) = max(T.Value(grps==u_grps(i)));
end
T.MaximumValue = max_vals(grps);
writetable(T(:,'MaximumValue'), 'test2.xlsx', 'Range', 'D1')
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
You may need to close the excel file before running this code.
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
Try this. You may need to verify if the output is correct
T = readtable('test4.xlsx');
T = table2cell(T(:,1:3));
temp = num2str([T{:,1}].', 16);
temp = mat2cell(temp, ones(size(temp,1),1), size(temp,2));
[~, ~, grps] = unique(strcat(temp, T(:,2)));
u_grps = unique(grps);
max_vals = zeros(numel(u_grps), 1);
for i=1:numel(u_grps)
max_vals(i) = max([T{grps==u_grps(i), 3}]);
end
T = table(max_vals(grps), 'VariableNames', {'MaximumValue'});
writetable(T(:,'MaximumValue'), 'test4.xlsx', 'Range', 'D1')
SOMNATH MAHATO
il 28 Mag 2020
SOMNATH MAHATO
il 28 Mag 2020
Ameer Hamza
il 28 Mag 2020
Yes, it seems possible, but the first column of the excel file is in some custom format, which is not recognized by MATLAB. You may first need to change the data type of the first column in Excel and then follow the code in my comment to only save each maximum value once.
SOMNATH MAHATO
il 28 Mag 2020
Categorie
Scopri di più su Data Import from MATLAB in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!