How to write strings and numbers to a text file

조회 수: 19 (최근 30일)
Shiva Teja Golla
Shiva Teja Golla 2019년 2월 5일
편집: cui,xingxing 2024년 4월 27일
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
KSSV
KSSV 2019년 2월 5일
USe Strcat to get the strings and write them using fprintf
Shiva Teja Golla
Shiva Teja Golla 2019년 2월 5일
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.

댓글을 달려면 로그인하십시오.

답변 (3개)

Suryaansh Mata
Suryaansh Mata 2019년 6월 18일
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 2019년 6월 18일
편집: Jan 2019년 6월 18일
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 2022년 9월 5일
편집: cui,xingxing 2024년 4월 27일
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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by