How to import text file and sort it into arrays
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
Robert U
il 9 Ott 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
Categorie
Scopri di più su Characters and Strings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!