Effacer les filtres
Effacer les filtres

Text File read and write out

49 vues (au cours des 30 derniers jours)
Thuan
Thuan le 24 Mai 2017
Commenté : Walter Roberson le 25 Mai 2017
Hey guys, I'm new to Matlab, and hoping I can get some some help. I'm trying to read in a text file and write it to the format I want. fileread('file') will give me the following data
1356297 2 0.0521214521 0.0005944170 0.0016546078 0.0125977102
4197358 89 0.0523557459 0.1244454656 -0.164303258 -0.542853886
3679865 104 0.0540524244 -0.015693102 -0.004209815 0.0009847531
I want it to write this data to the following format.
"Node 1356297", "Node 4197358", "Node 3679865"
"<0.0005944170, 0.0016546078, 0.0125977102>", @
"<0.1244454656, -0.164303258, -0.542853886>", @
"<-0.015693102, -0.004209815, 0.0009847531>", @
Thanks, Thuan
  2 commentaires
Fernando Fernandes
Fernando Fernandes le 24 Mai 2017
I think you may find your answer here:
https://www.mathworks.com/help/matlab/ref/textread.html
Walter Roberson
Walter Roberson le 24 Mai 2017
textread is not recommended; it has been pretty much replaced by textscan or other routines.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 24 Mai 2017
With your regular structure and no headers, you can use load() to read the file.
data = load('file');
ids = data(:,1);
vecs = data(:,4:end);
fid = fopen('Output.txt', 'wt');
fmt = [strjoin( repmat( {'"Node %d"'}, 1, length(ids), ', '), '\n'];
fprintf(fmt, ids);
fmt = ['"<', strjoin( repmat( {'%f'}, 1, size(vecs,2) ), ','), '>" @\n'];
fprintf(fmt, vecs.'); %transpose is important
fclose(fid);
  2 commentaires
Thuan
Thuan le 25 Mai 2017
Modifié(e) : Walter Roberson le 25 Mai 2017
How would I do it if I have a heading let say
MSC.Patran 21.1.348049 Fri Jul 03 23:12:17 PDT 2015 - Analysis Code: MSC.Nastran
Load Case: 653215
Result
1356297 2 0.0521214521 0.0005944170 0.0016546078 0.0125977102
Walter Roberson
Walter Roberson le 25 Mai 2017
If I correctly count there as being 4 header lines, then,
fid = fopen('file', 't');
data = cell2mat( textscan(fid, '', 'HeaderLines', 4, 'CollectOutput', true) );
fclose(fid);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export 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