📄 xsortrec.m
字号:
% xsortrec.m
% Scope: This MATLAB program reads a data file (each row is a record),
% sorts the records by the elements of a specified column in
% ascending order, and writes the result into an external file.
% Usage: xsortrec
% Inputs: - name of the input file
% - index of the column used in ordering the records
% - name of the output file (optional)
% Outputs: - ordered data records on the specified file or on screen
% Remarks:
% The format of the output file should be changed to reflect
% the number and data types used in each record (the default is
% for the structure used in file data2.dat).
% Last update: 06/27/00
% Copyright (C) 1998-00 by LL Consulting. All Rights Reserved.
clear
yes = 'y';
disp(' ');
% Read the input data file
f1 = input('Specify the input filename (with extension), e.g. data2.dat --> ','s');
disp(' ');
tt = load(f1);
[nrow,ncol] = size(tt);
z = zeros(nrow,ncol);
disp(['The input array ' f1 ' has ' num2str(ncol) ' columns']);
disp(' ');
% Specify the column used to arrange the records in ascending order
kcol = input('Specify the column index used for sorting --> ');
disp(' ');
x = tt(:,kcol);
% Sort the records by the value of the elements in selected column,
% in ascending order
[y,index] = sort(x);
for k = 1:nrow
z(k,:) = tt(index(k),:);
end
% Save the generated data into a specified file if desired
answer2 = input('Do you want to save the generated data? (y/n)[y] --> ','s');
disp(' ');
if isempty(answer2)
answer2 = 'y';
end
if strcmp(answer2,yes) == 1
f2 = input('Specify the output file name --> ','s');
else
f2 = 1; % output to the screen
end
% Save results into an external file. The format of the output file
% should be changed to reflect the number and data types used in
% each record. The default is for the structure used in file data2.dat.
for k = 1:nrow
fprintf(f2,'%d %12.7f %12.7f %8.0f %d %d %14.7f \n',z(k,1:ncol));
end
disp(' ');
disp('End of the program XSORTREC ');
disp(' ');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -