Read data from text file. Tables with uneven number of rows

6 vues (au cours des 30 derniers jours)
Auwais Ahmed
Auwais Ahmed le 10 Fév 2022
Commenté : Turlough Hughes le 11 Fév 2022
I have data of following shape.
I need to save all the numerical values in tables from the file as matrices. The problem is that all the tables have different number of rows. What would be the best way to go about solving the problem. Thanks
  3 commentaires
Auwais Ahmed
Auwais Ahmed le 10 Fév 2022
This is the file. Thanks
Benjamin Thompson
Benjamin Thompson le 10 Fév 2022
How do you want the data combined together? If you combine everything into a single table then you will have rows from different trajectories. Is that OK? Can you provide more information about what you want?

Connectez-vous pour commenter.

Réponses (1)

Turlough Hughes
Turlough Hughes le 11 Fév 2022
Modifié(e) : Turlough Hughes le 11 Fév 2022
fid = fopen('t1.txt');
c = 1;
while ~feof(fid)
d = fgetl(fid);
if ~isempty(regexp(d,'X.*Y.*Z.*Energy', 'once'))
part = textscan(fid,'%f%f%f%f');
data{c} = [part{:}];
c = c+1;
end
end
fclose(fid);
This uses fgetl to find the headers, then textscan reads the arrays.
  2 commentaires
Stephen23
Stephen23 le 11 Fév 2022
Replace the WHILE and IF with the simpler
while ~feof(fid)
...
end
Turlough Hughes
Turlough Hughes le 11 Fév 2022
Thanks Stephen, I've edited the answer.

Connectez-vous pour commenter.

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by