how to sort each line in a text file by the first character in a line?

6 views (last 30 days)
how do I get a switch case loop that reads a text file line by line and sorts the lines by the beginning character in each line? I need to use a switch case to account for four different characters. I'm using the fgetl function to read the file line by line so now I just need to figure out how to sort the lines by the beggining character. Once I can sort them I will likely store them in different variables.
  4 Comments
Engineer_guy
Engineer_guy on 11 Jul 2020
Please see my comment below where I provide a shortened version of the text file along with more details.
Steven Lord
Steven Lord on 20 Oct 2020
Copying question in case user edits it away as was the case with several previous questions.
"how do I get a switch case loop that reads a text file line by line and sorts the lines by the beginning character in each line? I need to use a switch case to account for four different characters. I'm using the fgetl function to read the file line by line so now I just need to figure out how to sort the lines by the beggining character. Once I can sort them I will likely store them in different variables."

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 11 Jul 2020
The data you posted is a bit difficult to work with, so I created my own version that should work with it. It assumes your data are in a cell array.
Try this:
C = {'G34 706.0461e-003 823.4578e-003 438.7444e-003 489.7644e-003 276.0251e-003'
'G34 31.8328e-003 694.8286e-003 381.5585e-003 445.5862e-003 679.7027e-003'
'R01 276.9230e-003 317.0995e-003 765.5168e-003 646.3130e-003 655.0980e-003'
'R01 46.1714e-003 950.2220e-003 795.1999e-003 709.3648e-003 162.6117e-003'
'R01 97.1318e-003 34.4461e-003 186.8726e-003 754.6867e-003 118.9977e-003'};
FC = cellfun(@(x)x(:,1), C); % Get First Letters
[Cu,~,idx] = unique(FC, 'stable'); % Unique Letters % Indices
Sorted = accumarray(idx, (1:numel(idx)).', [], @(x){C(x)}); % Separate By First LEtter Into Each Cell Of ‘Sorted’
OutG = Sorted{1} % Display Result
OutR = Sorted{2} % Display Result
producing:
OutG =
2×1 cell array
{'G34 706.0461e-003 823.4578e-003 438.7444e-003 489.7644e-003 276.0251e-003'}
{'G34 31.8328e-003 694.8286e-003 381.5585e-003 445.5862e-003 679.7027e-003' }
OutR =
3×1 cell array
{'R01 276.9230e-003 317.0995e-003 765.5168e-003 646.3130e-003 655.0980e-003'}
{'R01 46.1714e-003 950.2220e-003 795.1999e-003 709.3648e-003 162.6117e-003' }
{'R01 97.1318e-003 34.4461e-003 186.8726e-003 754.6867e-003 118.9977e-003' }
This should work with your data. It is not restricted to simply those letters you posted, and can work with amy set.
.

More Answers (1)

Image Analyst
Image Analyst on 11 Jul 2020
I don't understand what those 4 letters are for. And you forgot to attach your text file. Why not just use fgetl() to read all the lines into a cell array and then call sort()?
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd; % or 'C:\wherever';
if ~isfolder(startingFolder)
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
lines{lineCounter} = textLine;
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);
% Now sort by first character:
sortedLines = sort(lines')
fprintf('\nDONE running %s.m ...\n', mfilename);
  4 Comments
Engineer_guy
Engineer_guy on 11 Jul 2020
Edited: Engineer_guy on 11 Jul 2020
they are not on a single line. There is a set number of lines and set number of elements btwn each data field. These are listed above. I had trouble attaching the file it was too large so instead I figured we can just make it work for this portion of the text file and I can easily scale up the code from there. SInce the number of elements and lines in each data field do not change.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!