Filter löschen
Filter löschen

How to create a text file in a specific form of data arrangement

2 Ansichten (letzte 30 Tage)
Hello, I'd like to know how to create a text file that can be used as input to another program
For example, this is my data
x = [1 2 3 4 5];
q = [6 7 8 9 10];
The text file should be in the form like this
x 5
1 2 3 4 5
Data
6 7 8 9 10
All data should be displayed in a row not column form.
The headers are x 5 and Data
*5 is the data count
I tried using frprint and read all the documents related but I still don't know how to do it.
I'm very new to MATLAB. Any help would be greatly appreciated.

Akzeptierte Antwort

KSSV
KSSV am 4 Okt. 2017
x = [1 2 3 4 5];
q = [6 7 8 9 10];
fid = fopen('data.txt','w') ;
fprintf(fid,'%s %d\n','x',length(x)) ;
fprintf(fid,'\n') ;
fprintf(fid,[repmat('%f ',1,length(x)),'\n'],x') ;
fprintf(fid,'\n') ;
fprintf(fid,'%s \n','Data') ;
fprintf(fid,'\n') ;
fprintf(fid,[repmat('%f ',1,length(q)),'\n'],q') ;
fclose(fid) ;

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by