How can I read array from a text file?
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Angel Dimitrov
il 21 Mar 2020
Commentato: Angel Dimitrov
il 22 Mar 2020
I have a lot of text files in the form:
(0000000000)
(2020231032)
(0222320033)
(3120030223)
(0001132233)
And I want to import them as arrays. When I get rid of the clammers and use dlmread, it reads each row as one big number, but I need them as row vectors of the given length. Is there a way to read the file as the array I wish, or maybe a function that inserts comma after every number?
2 Commenti
Ameer Hamza
il 21 Mar 2020
How do you want to load each row into array? Giving an example output based on the lines in question will be helpful
Risposta accettata
Ameer Hamza
il 21 Mar 2020
Modificato: Ameer Hamza
il 21 Mar 2020
Try this
file = fopen('filename.txt');
data = textscan(file, '(%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d)');
fclose(file);
data = cell2mat(data);
3 Commenti
Ameer Hamza
il 22 Mar 2020
Following code will work on arbitrary number of digits
file = fopen('temp.txt');
data = textscan(file, '%s');
fclose(file);
result = cell2mat(cellfun(@(x) sscanf(x(2:end-1), '%1d'), data{1}, 'UniformOutput', 0)')';
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Spreadsheets in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!