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

1 次查看(过去 30 天)
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.

采纳的回答

KSSV
KSSV 2017-10-4
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) ;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by