How to Export Table to Text File without quotations marks for each element?

23 visualizaciones (últimos 30 días)
Hi!
I created the following array:
C = {'[SITE]';'station7025250_baseline1';'[LAT, log and ALT]'};
Then I converted it to a table by:
T=cell2table(C)
Then I saved it as:
writetable(T,'C:\Users\test.txt', 'WriteVariableNames', false, 'Delimiter', 'space')
And I got the following in the test.txt file:
"[size]"
"station7025250_baseline1"
"[LAT, log and ALT]"
I am struggling to get rid of the " " (quotation marks)
Any suggestions would be greatly appreciated! I need to create around 2500 test files like this one to be read by a Weather Generator (LARS-WG).
HOWEVER, If anyone knows how to change information in a .st file and save the changes using MATLAB would solve my problem from the root, as I won't have to manually save the .txt files into .st using notepad++, but rather save them directly into .st files.
Thanks in advance,
Pablo

Respuestas (2)

per isakson
per isakson el 11 de Mayo de 2016
Editada: per isakson el 12 de Mayo de 2016
AFAIK: that's the way writetable works.
Try this alternative, which prints the cell array with fprintf and avoids table
>> filespec = 'h:\m\cssm\cssm.txt';
>> cell_array = {'[SITE]';'station7025250_baseline1';'[LAT, log and ALT]'};
>> cssm( cell_array, filespec )
>> type( filespec )
[SITE]
station7025250_baseline1
[LAT, log and ALT]
where
function cssm( cell_array, filespec )
fid = fopen( filespec, 'wt' );
for jj = 1 : length( cell_array )
fprintf( fid, '%s\n', cell_array{jj} );
end
fclose( fid );
end
"HOWEVER, If anyone knows how to change information in a .st file and save the changes using MATLAB" &nbsp I found this and guess that's very doable.
However, make it easy for us and
  • upload a sample pair of a txt-file (input) and a st-file (output). (Or whichever is input and output, respectively.)
  • describe "the changes" you need in some detail.
&nbsp
In response to comment
The trick is to implement "the given templates" as format specifiers in Matlab.
Your comment is easy to understand and I believe I got it right.
Here are two functions, write_st_file and write_sce_file together with examples on how to call them. (They miss comments and error handling.)
This example shows how to call write_st_file
st_filespec = fullfile( 'h:\m\cssm', 'test.st' );
site = 's1_m2_g6';
location = [ 45.375, 286.125, -99 ];
w_filespec = fullfile( 'h:\m\cssm', 's1_m2_g6.dat' );
write_st_file( st_filespec, site, location, w_filespec )
type( st_filespec )
outputs
[SITE]
s1_m2_g6
[LAT, LON and ALT]
45.375000 286.125000 -99.000000
[WEATHER FILES]
h:\m\cssm\s1_m2_g6.dat
[FORMAT]
YEAR JDAY MIN MAX RAIN
[END]
where
function write_st_file( filespec, site, location, weather_file )
fmt = cat( 2, '[SITE]\n' , '%s\n' ...
, '[LAT, LON and ALT]\n' , '\t%f\t%f\t%f\n'...
, '[WEATHER FILES]\n' , '%s\n' ...
, '[FORMAT]\n' ...
, 'YEAR JDAY MIN MAX RAIN\n' ...
, '[END]\n' );
fid = fopen( filespec, 'wt' );
fprintf( fid, fmt, site, location, weather_file );
fclose( fid );
end
After looking at the file you uploaded I replaced '%f %f %f\n' by '\t%f\t%f\t%f\n'. Furthermore, you might need to replace '\n' by '\r\n' in both functions.
&nbsp
and this example shows how to call write_sce_file
sce_filespec = fullfile( 'h:\m\cssm', 'test.sce' );
name = 'CF_grid7_inmcm4_b1_f1_rcp45';
baseline = 1975;
future = 2025;
%
data = [ 0.91,1.02,1.01,1.53,1.02,0.91
1.11,1.03,0.99,2.39,2.13,0.98
1.22,1.01,1.01,2.05,1.82,0.91
1.14,0.99,1.02,1.05,0.36,0.91
1.07,0.97,1.00,0.37,-0.16,0.97
0.99,0.98,0.99,0.37,0.20,1.10
1.04,1.00,0.99,0.77,0.57,1.00
1.00,1.01,1.00,0.26,0.35,1.11
1.00,1.02,1.00,0.44,0.32,1.04
0.93,1.00,0.99,0.45,0.30,0.98
0.95,1.04,1.00,0.38,0.62,0.95
1.22,1.02,0.99,1.30,0.64,0.98 ];
write_sce_file( sce_filespec, name, baseline, future, data )
type( sce_filespec )
outputs
[VERSION]
LARS-WG5.5
[NAME]
CF_grid7_inmcm4_b1_f1_rcp45
[BASELINE]
1975
[FUTURE]
2025
[GCM PREDICTIONS]
Jan 0.910 1.020 1.010 1.530 1.020 0.910
Feb 1.110 1.030 0.990 2.390 2.130 0.980
Mar 1.220 1.010 1.010 2.050 1.820 0.910
Apr 1.140 0.990 1.020 1.050 0.360 0.910
May 1.070 0.970 1.000 0.370 -0.160 0.970
Jun 0.990 0.980 0.990 0.370 0.200 1.100
Jul 1.040 1.000 0.990 0.770 0.570 1.000
Aug 1.000 1.010 1.000 0.260 0.350 1.110
Sep 1.000 1.020 1.000 0.440 0.320 1.040
Oct 0.930 1.000 0.990 0.450 0.300 0.980
Nov 0.950 1.040 1.000 0.380 0.620 0.950
Dec 1.220 1.020 0.990 1.300 0.640 0.980
[END]
where
function write_sce_file( filespec, name, baseline, future, data )
fmt = cat( 2, '[VERSION]\n' , 'LARS-WG5.5\n' ...
, '[NAME]\n' , '%s\n' ...
, '[BASELINE]\n' , '%d\n' ...
, '[FUTURE]\n' , '%d\n' ...
, '[GCM PREDICTIONS]\n' );
months = { 'Jan','Feb','Mar','Apr','May','Jun' ...
, 'Jul','Aug','Sep','Oct','Nov','Dec' };
fid = fopen( filespec, 'wt' );
fprintf( fid, fmt, name, baseline, future );
for jj = 1 : length( months )
fprintf( fid, '%s', months{jj} );
fprintf( fid, '\t%.3f', data(jj,:) );
fprintf( fid, '\n' );
end
fprintf( fid, '[END]\n' );
fclose( fid );
end
Afterthought. If the row header, months, is not always the same it's better to make it an input argument and call it row_header. Does the row headers belong to the "template" or is it data? And one can argue that it belongs together with data.
  2 comentarios
Pablo Jaramillo Restrepo
Pablo Jaramillo Restrepo el 11 de Mayo de 2016
Thanks for the prompt response!
So basically I need to generate .st files with the following template only changing the numbers in the lines with "(CHANGE)" using a for loop to generate hundreds of these .st files:
[SITE]
s1_m2_g6 (CHANGE)
[LAT, LON and ALT]
45.375 286.125 -99 (CHANGE)
[WEATHER FILES]
s1_m2_g6.dat (CHANGE)
[FORMAT]
YEAR JDAY MIN MAX RAIN
[END]
Also I need to generate .sce files with the following template changing only the data under the blocks [NAME], [BASELINE], [FUTURE], [GCM PREDICTIONS].
The NAME, BASELINE and FUTURE are coming from a for loop. The GCM PREDICTIONS data is coming from a .mat files created through a parallel for loop for hundreds of iterations, thus needing to create hundreds of .sce files.
[VERSION]
LARS-WG5.5
[NAME]
CF_grid7_inmcm4_b1_f1_rcp45 (CHANGE)
[BASELINE]
1975 (CHANGE)
[FUTURE]
2025 (CHANGE)
[GCM PREDICTIONS]
Jan 0.91 1.02 1.01 1.53 1.02 0.91 (CHANGE...)
Feb 1.11 1.03 0.99 2.39 2.13 0.98
Mar 1.22 1.01 1.01 2.05 1.82 0.91
Apr 1.14 0.99 1.02 1.05 0.36 0.91
May 1.07 0.97 1.00 0.37 -0.16 0.97
Jun 0.99 0.98 0.99 0.37 0.20 1.10
Jul 1.04 1.00 0.99 0.77 0.57 1.00
Aug 1.00 1.01 1.00 0.26 0.35 1.11
Sep 1.00 1.02 1.00 0.44 0.32 1.04
Oct 0.93 1.00 0.99 0.45 0.30 0.98
Nov 0.95 1.04 1.00 0.38 0.62 0.95
Dec 1.22 1.02 0.99 1.30 0.64 0.98 (...CHANGE)
[END]
I know how to work with the for loops to generate the imputs (i.e: what needs to be changed in each file). What I don't know is how to create a .st or.sce file from MATLAB using the given templates.
I am attaching the original .st and .sce files for reference.

Iniciar sesión para comentar.


Tao Zhan
Tao Zhan el 10 de Abr. de 2022
writetable(T,'C:\Users\test.txt', 'WriteVariableNames', false, 'Delimiter', 'space', 'QuoteStrings','none')

Community Treasure Hunt

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

Start Hunting!

Translated by