How to import text file and sort it into arrays

9 vues (au cours des 30 derniers jours)
Behzad Parvin
Behzad Parvin le 9 Oct 2018
Hello, I have a text file that starts with some infos and then the serial number of a meter and then codes for activating of it. I want to sort the data in one matrix in a way that the first column would be SExxxxxxx (serial number) and the next columns the code for the respective Serial number. every meter has 33 codes. So finally we have a matrix of 10 or 12 rows and 34 columns. I was thinking about using the textscan for sorting the matrix. but I dont know how to start searching the file for the serial number and start sorting them, from let's say when he finds '[SE' put SExxxxx in the first column and the 33 next lines of the file put it in the next columns.

Réponse acceptée

Guillaume
Guillaume le 9 Oct 2018
Here is how I'd do it:
codetoks = repmat('\s*([A-Z]{2})=([A-Z0-9]*)', 1, 33); %regexp to tokenize the 33 XX = ZZZZZ
serialtoks = '\[([^\]]*)]'; %regexp to tokenize the serial number
filecontent = fileread('SE Sample.txt'); %read file
tokens = regexp(filecontent, [serialtoks, codetoks], 'tokens'); %extract tokens
tokens = vertcat(tokens{:}); %concatenate into a Nx67 array
%see note
codes = unique(tokens(:, 2:2:end)); %extract all unique codes (the XX)
[~, column] = ismember(tokens(:, 2:2:end), codes);
codevalues = cell(size(column));
codevalues(sub2ind(size(codevalues), repmat(1:size(tokens, 1), size(codevalues, 2), 1)', column)) = tokens(:, 3:2:end);
%end note
out = cell2table([tokens(:, 1), codevalues], 'VariableNames', ['Serial'; codes])
Note that the code between see note and end note is there to ensure that if the activation codes don't come in the same order they still end up in the right column. If you are sure that they're always in the same order and always present, then you can replace that whole section with:
codes = tokens(1, 2:2:end)';
codevalues = tokens(:, 3:2:end);

Plus de réponses (1)

Robert U
Robert U le 9 Oct 2018
Hi Behzad Parvin,
please have a look if this piece of code serves your needs:
fileID = fopen('SE Sample.txt');
PlainText = textscan(fileID,'%s');
fclose(fileID);
PlainText = PlainText{1};
StartSE = find(cellfun(@(cIn) isequal(cIn,1),strfind(PlainText,'[SE')));
CellSE = arrayfun(@(nElem) PlainText(StartSE(nElem):StartSE(nElem+1)-1),1:length(StartSE)-1,'UniformOutput',false);
CellSE = [CellSE{:}]';
CellSE = regexprep(regexprep(CellSE,'^[',''),']$','');
CellSE = regexprep(CellSE,'^\w\w=','');
Kind regards,
Robert

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by