How to export matrices with a specific format to .txt?
Show older comments

I would like to export 2 matrices (wind_u and wind_v) to .txt file in a format similar to the picture. Here's my code, the problem is that when I open the .txt file, the format is not right
% open a file for writing
fid = fopen('wind.txt', 'w');
fprintf(fid, '#This file contains a 131x191 grid with wind data. \n');
if true
% code
end
for ii=1:1465
fprintf(fid, '%s \n' , 'wind_u');
for jj = 1:size(lat,1)
fprintf(fid,'%g\t', uwind(ii,jj,:));
fprintf(fid,'\n');
end
fprintf(fid, '%s \n', 'wind_v');
for kk = 1:size(lat,1)
fprintf(fid,'%g\t', vwind(ii,kk,:));
fprintf(fid,'\n');
end
end
fclose(fid)
1 Comment
You can help us be giving a bit more information:
- please upload some sample data that we can use (you can use the paperclip button, and then press both Choose file and Attach file).
- clearly explain what the problem is with the current code (e.g. wrong spacing, line breaks in the wrong places, etc.).
- tell us exactly what variables you are using (numeric arrays, or date arrays, or something else), and also their dimensions.
Remember we cannot see your screen, so you need to help us by giving us all the required information to make this work or to try it out.
Answers (1)
Without any sample data to try out this is rather difficult to help you, but you can try this code (I created sample data based on the screenshot, but had to make some assumptions about the data sizes and class, because you did not give us any information on this). It adjusts automatically to the data size, as long as the orientation is the same.
% Sample data:
wind_u(:,:,2) = [-1.34,-1.39,-1.44; 1.36, -1.24, -99.99];
wind_u(:,:,1) = [-99.99, -2.3, 4.7; 4.2, 0.1, -1.1];
wind_v(:,:,2) = [0.54, 0.54, 0.59; -1.55, 3.64, -99.99];
wind_v(:,:,1) = [-99.99, 1.2, 0.67; 2.2, -4.1, -1.2];
time = [2003,01,29,06,00,00; 2003,01,29,12,00,00];
% Check array sizes:
S = size(wind_u);
assert(all(S==size(wind_v)),'Data arrays must be the same size')
assert(size(time,1)==S(3),'Data array third dimension corresponds to time.')
% Create format strings:
fmt_t = sprintf('time %s\n',repmat(' %02u',1,5));
fmt_w = sprintf('%s\n',repmat(' %6.2f',1,S(2)));
% Write to textfile:
fid = fopen('wind.txt', 'wt');
fprintf(fid, '#This file contains a %ux%u grid with wind data.\n',S(1),S(2));
for k = 1:size(time,1)
fprintf(fid,fmt_t, time(k,1:5));
fprintf(fid,'%s\n','wind_u');
fprintf(fid,fmt_w, wind_u(:,:,k).');
fprintf(fid,'%s\n','wind_v');
fprintf(fid,fmt_w, wind_v(:,:,k).');
end
fclose(fid);
This creates the following textfile:
#This file contains a 2x3 grid with wind data.
time 2003 01 29 06 00
wind_u
-99.99 -2.30 4.70
4.20 0.10 -1.10
wind_v
-99.99 1.20 0.67
2.20 -4.10 -1.20
time 2003 01 29 12 00
wind_u
-1.34 -1.39 -1.44
1.36 -1.24 -99.99
wind_v
0.54 0.54 0.59
-1.55 3.64 -99.99
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!