write a loop reading txt files to write a xlsx file with specific strings and numbers
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I have txt files called: Input_1.txt, Input_2.txt, and so on. In these files I have both numbers and strings.
For example, I have to create a loop in wich I have to select, for each txt file, the string at the position (3,1) (in the attached example the string "hello1" and "hello2"), and the number at the position (5,2)
Then, I want to obtain a xlsx files Output.xlsx in which I have the selected strings in the first column and the selected numbers in the second column.
See please the attached files.
Thank you
S = dir('Input_*.txt');
for k = 1:numel(S)
S(k).name
end
0 commentaires
Réponse acceptée
darova
le 4 Mai 2020
try this
S = dir('Input_*.txt');
str = {};
num = {};
for k = 1:numel(S)
A = importdata(S(k).name);
str{k} = A{3,1};
num{k} = A{5,2}'
end
num = cell2mat(num);
11 commentaires
darova
le 6 Mai 2020
Another attempt
S = dir('input*.txt');
str = {};
num = {};
for k = 1:numel(S)
A = readtable(S(k).name,'delimiter','\t','readVariableNames',false);
str(k) = table2cell(A(3,2));
num(k) = table2cell(A(6,2));
end
num = cell2mat(num);
num(isnan(num)) = 100;
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets 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!