指定のテキストファイルを生成する方法

33 visualizaciones (últimos 30 días)
修
el 20 de Dic. de 2022
Comentada: el 27 de Dic. de 2022
ある計測装置に取り込むデータ形式として,複素数を以下のように記述されたテキストファイルで生成する必要があります
(10,8)
(9,7)
(8,6)
この形式にmatlabで演算されたN×1複素配列を変換してテキストファイルで出力したいのですが、
特殊な文字列を含む形式で出力するはどうしたらいいでしょうか
強引に実部と虚部を分割したN×2の配列を作り、文字列に変換してからファイルを生成するよう組んでみましたが、そのままだとchr型となり、writematrixでテキスト保存すると余計な記号がついてしまいます。
Expdata=sprintf('(%s,%s)\n',data(:,1),data(:,2));
どのようにしたら希望の形式が作れますでしょうか。

Respuesta aceptada

修
el 23 de Dic. de 2022
お二人ともありがとうございます。
いろいろと検証していたのですが、どうもfprintfでそれぞれの結果をまとめる段階で実部,虚部という並びになっていないようなのですが、なぞそうなったのかがわかりません。
下は実部を1,虚部を2として実行してみた結果です。
% データの用意
N = 10;
X = ones(N,1);
data(1:N,1) = X;
data(1:N,2) = 2*X;
% データ書き込み
formatSpec = "(%d,%d)\n";
fileID = fopen("test1.txt","w"); % 出力ファイルオープン
fprintf(fileID,formatSpec,data(:,1),data(:,2));
fclose(fileID);
type("test1.txt");
(1,1)
(1,1)
(1,1)
(1,1)
(1,1)
(2,2)
(2,2)
(2,2)
(2,2)
(2,2)
  1 comentario
Hiro Yoshino
Hiro Yoshino el 23 de Dic. de 2022
すみません、fprintf のような低レベル水準関数だと一行ずつ for loop しないとだめですね。
仕切り直して:
N = 10;
X = rand(N,1,"like",1i+1)
X =
0.9154 + 0.9243i 0.0341 + 0.6117i 0.5942 + 0.3965i 0.0738 + 0.4764i 0.1970 + 0.3740i 0.0839 + 0.8642i 0.1147 + 0.6350i 0.2431 + 0.8951i 0.0952 + 0.7784i 0.8427 + 0.8234i
data(1:N,1) = real(X);
data(1:N,2) = imag(X);
data
data = 10×2
0.9154 0.9243 0.0341 0.6117 0.5942 0.3965 0.0738 0.4764 0.1970 0.3740 0.0839 0.8642 0.1147 0.6350 0.2431 0.8951 0.0952 0.7784 0.8427 0.8234
複数行で行う場合は@Akira Agataさんが示して下さった compose が良いですね:
C =compose("(%e, %e)",data(:,1),data(:,2))
C = 10×1 string array
"(9.154106e-01, 9.243007e-01)" "(3.409286e-02, 6.116904e-01)" "(5.942270e-01, 3.964775e-01)" "(7.384291e-02, 4.763674e-01)" "(1.969906e-01, 3.739939e-01)" "(8.386865e-02, 8.642191e-01)" "(1.147138e-01, 6.350207e-01)" "(2.430560e-01, 8.951343e-01)" "(9.516310e-02, 7.784384e-01)" "(8.427108e-01, 8.234494e-01)"
writelines(C,"test1.txt");
表示
type("test1.txt")
(9.154106e-01, 9.243007e-01) (3.409286e-02, 6.116904e-01) (5.942270e-01, 3.964775e-01) (7.384291e-02, 4.763674e-01) (1.969906e-01, 3.739939e-01) (8.386865e-02, 8.642191e-01) (1.147138e-01, 6.350207e-01) (2.430560e-01, 8.951343e-01) (9.516310e-02, 7.784384e-01) (8.427108e-01, 8.234494e-01)
という感じで良いかと思います。

Iniciar sesión para comentar.

Más respuestas (2)

Hiro Yoshino
Hiro Yoshino el 20 de Dic. de 2022
ひょっとしたらもう少しスマートな方法が有るかも知れませんが....
% データの用意
N = 10;
X = rand(N,1,"like",1i+1);
data(1:N,1) = real(X);
data(1:N,2) = imag(X)
data = 10×2
0.2797 0.3117 0.7102 0.6884 0.2867 0.7430 0.6207 0.1206 0.5966 0.7333 0.4319 0.0527 0.9783 0.2129 0.6727 0.2216 0.5665 0.2075 0.9452 0.0006
% データ書き込み
formatSpec = "(%s,%s)\n";
fileID = fopen("test1.txt","w"); % 出力ファイルオープン
fprintf(fileID,formatSpec,data(:,1),data(:,2));
fclose(fileID);
type("test1.txt");
(2.797037e-01,7.101982e-01) (2.867208e-01,6.207492e-01) (5.966407e-01,4.318941e-01) (9.782568e-01,6.727409e-01) (5.664620e-01,9.452370e-01) (3.116674e-01,6.883858e-01) (7.429645e-01,1.205636e-01) (7.333456e-01,5.272098e-02) (2.129227e-01,2.215730e-01) (2.075361e-01,6.287421e-04)
こんな感じでどうでしょうか?
  1 comentario
Akira Agata
Akira Agata el 21 de Dic. de 2022
Editada: Akira Agata el 21 de Dic. de 2022
+1
composewritelines を使っても同じことができます。
% データの用意
N = 10;
X = rand(N, 1, "like", 1i+1);
% 書き込みたい文字列形式に変換
C = compose("(%e, %e)", real(X), imag(X));
% テキストファイルとして出力
writelines(C, "test2.txt")

Iniciar sesión para comentar.


修
el 26 de Dic. de 2022
ご返信ありがとうございます。
writelines関数が動作しなかったため調べてみたのですが、当方の環境2021aでは2022aより実装のwritelines関数は使えないようでした。
質問のとおりwritematrixなどの出力関数ではダブルクォーテーションマークなど記述形式が限定されていますので、現環境では@Hiro Yoshino様がコメントされたfprintfをループ関数で回す方法しかないのかもしれません…。
  2 comentarios
Hiro Yoshino
Hiro Yoshino el 26 de Dic. de 2022
Editada: Hiro Yoshino el 26 de Dic. de 2022
訂正します。こんな風にすれば良いかと:
N = 10;
X = rand(N,1,"like",1i+1);
data(1:N,1) = real(X);
data(1:N,2) = imag(X);
data
data = 10×2
0.3739 0.2092 0.4644 0.0841 0.5628 0.5548 0.5082 0.0280 0.1743 0.7743 0.2142 0.1138 0.8427 0.1187 0.2465 0.6259 0.9038 0.7363 0.9088 0.7834
C =compose("(%e, %e)",data(:,1),data(:,2))
C = 10×1 string array
"(3.739472e-01, 2.091950e-01)" "(4.644203e-01, 8.408751e-02)" "(5.628343e-01, 5.547554e-01)" "(5.082131e-01, 2.799395e-02)" "(1.743209e-01, 7.743217e-01)" "(2.142454e-01, 1.138472e-01)" "(8.426814e-01, 1.186955e-01)" "(2.464870e-01, 6.259309e-01)" "(9.037843e-01, 7.362560e-01)" "(9.087814e-01, 7.833640e-01)"
% ファイル書き出し
fileID = fopen("test1.txt","w");
fprintf(fileID,"%s\n",C);
fclose(fileID);
% ファイルの中身表示
type("test1.txt")
(3.739472e-01, 2.091950e-01) (4.644203e-01, 8.408751e-02) (5.628343e-01, 5.547554e-01) (5.082131e-01, 2.799395e-02) (1.743209e-01, 7.743217e-01) (2.142454e-01, 1.138472e-01) (8.426814e-01, 1.186955e-01) (2.464870e-01, 6.259309e-01) (9.037843e-01, 7.362560e-01) (9.087814e-01, 7.833640e-01)
fprintf は行列も引数に取ることができるので、一発でいけます。
修
el 27 de Dic. de 2022
できました!
ありがとうございます。

Iniciar sesión para comentar.

Categorías

Más información sobre データのインポートとエクスポート en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!