How to sort and save data in same workspace / excel?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Muhammad Shaiful Bahri
am 13 Okt. 2019
Kommentiert: Walter Roberson
am 19 Nov. 2019
I have problem to save the data that i already sort by it Loading Port and Length. Im using for loop, the data that save in the final workpace is only the last sorted category, not the whole data.
data = readtable('Dummy.xlsx');
% LOADING
[Lport, a, LOADING] = unique(data(:,7));
LP = max(LOADING);
for G = 1 : LP
fprintf('\n\n\n\t\t\t\t\t\t\t\t\t <strong>LOADING PORT %d </strong>\n', G)
L = data(LOADING==G,:);
% display(data(LOADING==G,:))
[Length, b, LENGTH] = unique(L(:,3));
l = max(LENGTH);
for G = 1 : l
fprintf('\t\t\t\t\t\t\t\t\t<strong>LENGTH : Category %d</strong>\n',G)
length = L(LENGTH==G,:);
fprintf('\t\t\t\t\t\t\t\t\t <strong>SORTED WEIGHT</strong>\n')
[Weight, c] = unique(length.WEIGHT);
display(length(c,:))
end
end
The results in command window is here, but in workspace / variables on show the last loop / data.
LOADING PORT 1
LENGTH : Category 1
SORTED WEIGHT
NO CODE LENGTH HEIGHT WEIGHT SPECIAL LOADING UNLOADING
__ _________ ______ ______ ______ _______ _______ _________
1 'Alpha' 20 8 200 NaN 'AA' 'BB'
6 'Foxtrot' 20 8 222 NaN 'AA' 'BB'
2 'Bravo' 20 8 250 NaN 'AA' 'BB'
5 'Echo' 20 8 564 NaN 'AA' 'CC'
LENGTH : Category 2
SORTED WEIGHT
NO CODE LENGTH HEIGHT WEIGHT SPECIAL LOADING UNLOADING
__ _________ ______ ______ ______ _______ _______ _________
3 'Charlie' 40 8 360 NaN 'AA' 'BB'
4 'Delta' 40 8 950 NaN 'AA' 'CC'
LOADING PORT 2
LENGTH : Category 1
SORTED WEIGHT
NO CODE LENGTH HEIGHT WEIGHT SPECIAL LOADING UNLOADING
__ __________ ______ ______ ______ _______ _______ _________
10 'Juliet' 20 8 200 NaN 'BB' 'CC'
13 'Mike' 20 8 351 NaN 'BB' 'AA'
14 'November' 20 8 500 NaN 'BB' 'AA'
8 'Hotel' 20 8 569 NaN 'BB' 'CC'
9 'India' 20 8 752 NaN 'BB' 'CC'
LENGTH : Category 2
SORTED WEIGHT
NO CODE LENGTH HEIGHT WEIGHT SPECIAL LOADING UNLOADING
__ ______ ______ ______ ______ _______ _______ _________
12 'Lima' 40 8 245 NaN 'BB' 'AA'
7 'Golf' 40 8 248 NaN 'BB' 'CC'
11 'Kilo' 40 8 265 NaN 'BB' 'AA'
Here is the dummy data from the excel.
4 Kommentare
Walter Roberson
am 22 Okt. 2019
What is it you are trying to store? The formatted output? L? Length? b? LENGTH ? l? Weight? c?
Akzeptierte Antwort
Rajani Mishra
am 16 Okt. 2019
Hi,
In your code variables are updated in every iteration in both outer and inner loop, variables values are not saved in every iteration to any file whereas values stored in variables are overwritten.
Hence only last sorted values are saved. In the variable named ‘length’ only last sorted category value is saved and in the variable named ‘L’ last sorted port (both the categories) value is saved.
To save the whole data either write in a file (as you are already using ‘fprintf’ function) or keep adding rows to an existing table.
For writing to a file please refer: https://www.mathworks.com/help/matlab/ref/fprintf.html
For adding rows to an existing table please refer: https://www.mathworks.com/help/matlab/matlab_prog/add-and-delete-table-rows.html
Hope this helps!
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Files 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!