MATLAB Answers

0

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

Asked by Muhammad Shaiful Bahri on 13 Oct 2019
Latest activity Commented on by Muhammad Shaiful Bahri on 22 Oct 2019 at 6:02
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 Comments

Show 1 older comment
Here it is.
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
What is it you are trying to store? The formatted output? L? Length? b? LENGTH ? l? Weight? c?
i want to store 'length'. as the result at command window.

Sign in to comment.

1 Answer

Answer by Rajani Mishra on 16 Oct 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
Hope this helps!

  1 Comment

im sorry that i fail to adding row to the existing table. can give me some example?

Sign in to comment.