Clear Filters
Clear Filters

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

1 view (last 30 days)
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.

Accepted Answer

KSSV
KSSV on 4 Oct 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) ;

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!