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

조회 수: 1 (최근 30일)
Muhammad Shaiful Bahri
Muhammad Shaiful Bahri 2019년 10월 13일
댓글: Walter Roberson 2019년 11월 19일
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
Muhammad Shaiful Bahri 2019년 10월 22일
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월 21일
편집: 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개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by