How to write strings and numbers to a text file

19 Ansichten (letzte 30 Tage)
Shiva Teja Golla
Shiva Teja Golla am 5 Feb. 2019
Bearbeitet: cui,xingxing am 27 Apr. 2024
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 Kommentare
KSSV
KSSV am 5 Feb. 2019
USe Strcat to get the strings and write them using fprintf
Shiva Teja Golla
Shiva Teja Golla am 5 Feb. 2019
How to get this line
*TREAD,table_press_data_1,'press_data_1','csv','',0
This line incluse some text which shound be in ' '.
How to get this line in the required format.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Suryaansh Mata
Suryaansh Mata am 18 Jun. 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.

Jan
Jan am 18 Jun. 2019
Bearbeitet: Jan am 18 Jun. 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.

cui,xingxing
cui,xingxing am 5 Sep. 2022
Bearbeitet: cui,xingxing am 27 Apr. 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

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by