How to write strings and numbers to a text file
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I need to write a data whcih comprises strings and numbers to a text file (as shown below). The data is repetitive with some changes ( the text in bold changes every time). So, I want to use a loop to write this data to a text file . How can I do it through matlab...
Any help and ideas are appreciated....
*DIM,table_press_data_1,TABLE,6146,120,1,PN,PT
*TREAD,table_press_data_1,'press_data_1','csv','',0
*DIM,array_press_data_1,ARRAY,6146,120,1,,,
*DO,i,1,TMSTPNUM1,1
*VFUN,array_press_data_1(1,i),COPY,table_press_data_1(1,i)
*ENDDO
*DIM,table_press_data_2,TABLE,6146,120,1,PN,PT
*TREAD,table_press_data_2,'press_data_2','csv','',0
*DIM,array_press_data_2,ARRAY,6146,120,1,,,
*DO,i,1,TMSTPNUM2,1
*VFUN,array_press_data_2(1,i),COPY,table_press_data_2(1,i)
*ENDDO
*DIM,table_press_data_3,TABLE,6146,120,1,PN,PT
*TREAD,table_press_data_3,'press_data_3','csv','',0
*DIM,array_press_data_3,ARRAY,6146,120,1,,,
*DO,i,1,TMSTPNUM1,1
*VFUN,array_press_data_3(1,i),COPY,table_press_data_3(1,i)
*ENDDO
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
..........................................................
2 commentaires
Réponses (3)
Suryaansh Mata
le 18 Juin 2019
To write the data onto a file in MATLAB save the data as a string (concatenate using the 'strcat' function) and use the 'fprintf' command to write onto a text file. In order to incorporate a ' into your string use double ' , i.e. str = 'John''s' will store the string John's into the variable str.
0 commentaires
Jan
le 18 Juin 2019
Modifié(e) : Jan
le 18 Juin 2019
Do you really want to create:
*DIM,table_press_data_1,TABLE,6146,120,1,PN,PT
*TREAD,table_press_data_1,'press_data_1','csv','',0
*DIM,array_press_data_1,ARRAY,6146,120,1,,,
*DO,i,1,TMSTPNUM1,1
*VFUN,array_press_data_1(1,i),COPY,table_press_data_1(1,i)
*ENDDO
...
Or is "table_press_data_1" a placeholder for something?
To get the shown text:
pattern = [ ...
'*DIM,table_press_data_%d,TABLE,6146,120,1,PN,PT\n', ...
'*TREAD,table_press_data_%d,''press_data_%d'',''csv'','',0\n', ...
'*DIM,array_press_data_%d,ARRAY,6146,120,1,,, \n', ...
'*DO,i,1,TMSTPNUM1,1\n', ...
' *VFUN,array_press_data_%d(1,i),COPY,table_press_data_%d(1,i)\n', ...
'*ENDDO\n\n'];
[fid, msg] = fopen(FileName, 'w');
assert(fid ~= -1, 'Cannot open file %s: %s', FileName, msg);
for k = 1:17
fprintf(fid, pattern, repmat(k, 1, 6));
% Perhaps this is faster:
% fwrite(fid, strrep(pattern, '%d', sprintf('%d', k)), 'char');
end
fclose(fid);
If "table_press_data_1" is a placeholder, please explain, in which format the real data are stored.
0 commentaires
cui,xingxing
le 5 Sep 2022
Modifié(e) : cui,xingxing
le 27 Avr 2024
hi, if you use latest matlab 2022a, you can use new matlab build-in function WRITELINES
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you!
Email: cuixingxing150@gmail.com
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!