How can I write data to a text file using the symbol '/' in the end?

6 ビュー (過去 30 日間)
Paschalis Garouniatis
Paschalis Garouniatis 2017 年 10 月 31 日
コメント済み: Walter Roberson 2017 年 10 月 31 日
As the header implies i need to write data from a matrix to a text file and in the end of each line there must be the symbol '/'. Example of the needed output:
1 2 /
1 2 /
1 2 /
I have already written the script and used dlmwrite to create the text file. If it is possible i would like to keep dlmwrite. Any help will be appreciated. Thanks in advance.

採用された回答

per isakson
per isakson 2017 年 10 月 31 日
編集済み: per isakson 2017 年 10 月 31 日
"keep dlmwrite" That's not possible - AFAIK
Doc says: dlmwrite(filename,M) writes numeric data in array M to an ASCII format file.
This is one way
>> sprintf('%1d %1d %1c', 1,2,'/')
ans =
1 2 /
>>
Replace sprintf by fprintf
  2 件のコメント
Paschalis Garouniatis
Paschalis Garouniatis 2017 年 10 月 31 日
Thank you for your answer.
Walter Roberson
Walter Roberson 2017 年 10 月 31 日
Using dlmwrite for this is possible, it just isn't worth it.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 10 月 31 日
編集済み: Walter Roberson 2017 年 10 月 31 日
In order to use dlmwrite for this purpose, you will need to convert all of your data to a cell array in which each entry is an individual character, including entries for any separating spaces. Then you need to dlmwrite() specifying '' (the empty string) as the delimiter, and specify the precision parameter:
For example for the above output you would need something like
data = [1, 2; 1, 2; 1, 2];
temp = num2cell( num2str(data, '%d %d /'));
dlmwrite('TheFilename.txt', temp, '', 'Precision', '%s')

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by