How to write strings and numbers to a text file

139 次查看(过去 30 天)
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
hi, if you use latest matlab 2022a, you can use new matlab build-in function WRITELINES

类别

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