How to write strings and numbers to a text file

139 ビュー (過去 30 日間)
Shiva Teja Golla
Shiva Teja Golla 2019 年 2 月 5 日
回答済み: cui,xingxing 2022 年 9 月 5 日
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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by