How to sort and save data in same workspace / excel?

2 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2019-10-22
What is it you are trying to store? The formatted output? L? Length? b? LENGTH ? l? Weight? c?
Muhammad Shaiful Bahri
i want to store 'length'. as the result at command window.

请先登录,再进行评论。

采纳的回答

Rajani Mishra
Rajani Mishra 2019-10-16
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
Hope this helps!
  2 个评论
Muhammad Shaiful Bahri
编辑:Muhammad Shaiful Bahri 2019-10-22
im sorry that i fail to adding row to the existing table. can give me some example?
Walter Roberson
Walter Roberson 2019-11-19
The link about add-and-delete-table-rows has three examples.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by