Azzera filtri
Azzera filtri

Text File read and write out

49 visualizzazioni (ultimi 30 giorni)
Thuan
Thuan il 24 Mag 2017
Commentato: Walter Roberson il 25 Mag 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 Commenti
Fernando Fernandes
Fernando Fernandes il 24 Mag 2017
I think you may find your answer here:
https://www.mathworks.com/help/matlab/ref/textread.html
Walter Roberson
Walter Roberson il 24 Mag 2017
textread is not recommended; it has been pretty much replaced by textscan or other routines.

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 24 Mag 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 Commenti
Thuan
Thuan il 25 Mag 2017
Modificato: Walter Roberson il 25 Mag 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 il 25 Mag 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);

Accedi per commentare.

Categorie

Scopri di più su Data Import and Export 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!

Translated by