3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【備忘録】MATLABでカンマ区切りテキストをcsvで保存する。

Posted at

MATLABには配列やテーブルをcsvなどで保存するために、csvwriteやwritematrixなどの関数が用意されている。
ただし、書き込む配列は$m\times n$のサイズでなければならず、以下のような異なる成分数の行が連結されたようなコンマ区切りcsvは作れない。(もしできるなら教えてください)

a,1
b,2
c,3,4,5,6
d,7,8,9

この時は、MATLABで以下のようにテキスト配列を作成し、

testtext = ['a', ',', num2str(1), newline
            'b', ',', num2str(2), newline
            'c', ',', num2str(3), ',', num2str(4), ',', num2str(5), ',', num2str(6), newline
            'd', ',', num2str(7), ',', num2str(8), ',', num2str(9)];

fopen, fprintf, fcloseを使ってcsvに書き込むと、異なる成分数の行が混在したcsvを作れる。

fid = fopen('test.csv','w');
fprintf(fid, testtext);
fclose(fid);

上の例ではテキストを全て作成してからcsvに書き込んだが、fprintfをループに入れて一単語ずつ書き込んでも良い。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?