実数配列と複素数配列を結合してcsv保存する方法

23 views (last 30 days)
mushi77
mushi77 on 29 Jan 2022
Commented: Hernia Baby on 30 Jan 2022
単純な質問でしたらすみません
2Dのとある解析で1×100のdouble配列の変数Xと1×100のcomplex double配列の変数Yをcat関数を用いて配列変数"Data"に結合して格納したのち、writematrix関数でcsvファイルに出力しました。
格納したデータをみるとXのデータ列が"100+0i"のように虚部がゼロの複素数形式で格納されてしまうのですが、
そのcsvデータはXのデータ列が実数表記、Yのデータ列が虚数表記という形が望ましく、やり方がわかりません。
結合時に変数Dataの形式が変数Yの形式に引っ張られるのはわかるのですが、どのようにしたらことなる形式が共存する形でファイル出力できますでしょうか。

Accepted Answer

Hernia Baby
Hernia Baby on 29 Jan 2022
cell型 or table型で保存してください。
clc,clear;
X = rand(100,1);
Y = rand(100,1) + 1i*rand(100,1);
table型で出力します
A = table(X,Y)
A = 100×2 table
X Y ________ _________________ 0.42498 0.31991+0.57968i 0.15359 0.24818+0.32131i 0.33948 0.36641+0.9838i 0.83059 0.62299+0.64774i 0.34923 0.76293+0.52075i 0.47179 0.91616+0.66075i 0.53972 0.10019+0.14733i 0.30315 0.53986+0.19233i 0.048652 0.063277+0.24251i 0.50379 0.71424+0.75704i 0.9936 0.74634+0.94234i 0.2426 0.81459+0.14469i 0.8071 0.029032+0.70781i 0.59549 0.63086+0.71984i 0.22182 0.75438+0.7118i 0.95809 0.062628+0.70825i
% writetable(A,'Sample.csv');
この場合だとcsvの最初に X, Yが入ります。
それが嫌な場合はcell型にします
B = table2cell(A)
B = 100×2 cell array
{[0.4250]} {[0.3199 + 0.5797i]} {[0.1536]} {[0.2482 + 0.3213i]} {[0.3395]} {[0.3664 + 0.9838i]} {[0.8306]} {[0.6230 + 0.6477i]} {[0.3492]} {[0.7629 + 0.5208i]} {[0.4718]} {[0.9162 + 0.6608i]} {[0.5397]} {[0.1002 + 0.1473i]} {[0.3032]} {[0.5399 + 0.1923i]} {[0.0487]} {[0.0633 + 0.2425i]} {[0.5038]} {[0.7142 + 0.7570i]} {[0.9936]} {[0.7463 + 0.9423i]} {[0.2426]} {[0.8146 + 0.1447i]} {[0.8071]} {[0.0290 + 0.7078i]} {[0.5955]} {[0.6309 + 0.7198i]} {[0.2218]} {[0.7544 + 0.7118i]} {[0.9581]} {[0.0626 + 0.7082i]}
% writecell(B,'Sample2.csv');
  1 Comment
Hernia Baby
Hernia Baby on 29 Jan 2022
今気づきましたが、Bは2行100列でしたね
B = B';
で転置できます

Sign in to comment.

More Answers (2)

Voss
Voss on 29 Jan 2022
Edited: Voss on 29 Jan 2022
[EDIT: My answer is overkill since you can just use writecell(), as some other answers say, but I'll leave it up anyway because it's never a bad thing to know about lower-level functions.]
Since it is a csv file, you can fairly easily write it byte-by-byte:
% First, make up some vectors: real X, complex Y
N = 10;
% X = 1:N;
% Y = (1:N)+(-1).^(1:N).*repelem(1:N/2,1,2)*1i;
X = randn(1,N);
Y = randn(1,N)+randn(1,N)*1i;
Y(3:3:end) = real(Y(3:3:end)); % make some of the Y's real, for testing
% Put them together
M = [X; Y];
rM = real(M);
iM = imag(M);
% Now write the matrix to a csv file.
% Have to write the real and imaginary parts separately.
% This doesn't write the imaginary part if it is 0, but you can easily
% modify it to write +0i in that case:
fid = fopen('XY.csv','w');
for i = 1:size(M,1)
for j = 1:size(M,2)
if iM(i,j) > 0
fprintf(fid,'%g+%gi,',rM(i,j),iM(i,j));
elseif iM(i,j) < 0
fprintf(fid,'%g%gi,',rM(i,j),iM(i,j)); % the minus sign will be in the imaginary part
else
fprintf(fid,'%g,',M(i,j));
end
end
fprintf(fid,'\n');
end
fclose(fid);
% Validation that it actually worked:
% Check the contents:
fid = fopen('XY.csv');
data = fread(fid);
fclose(fid);
disp(char(data.'));
-1.27239,-1.2251,1.70223,0.278742,-0.500929,0.886592,1.76668,-0.408351,0.618765,-0.42773, 0.859372+1.16638i,1.79219-0.816116i,1.60847,1.14375-0.812293i,0.1704+0.488466i,-2.2107,0.442635+0.796152i,-1.62369+0.0539132i,2.43123,-0.826245-0.255318i,
% Make sure readmatrix can read it ok:
M_test = readmatrix('XY.csv')
M_test =
-1.2724 + 0.0000i -1.2251 + 0.0000i 1.7022 + 0.0000i 0.2787 + 0.0000i -0.5009 + 0.0000i 0.8866 + 0.0000i 1.7667 + 0.0000i -0.4084 + 0.0000i 0.6188 + 0.0000i -0.4277 + 0.0000i 0.8594 + 1.1664i 1.7922 - 0.8161i 1.6085 + 0.0000i 1.1438 - 0.8123i 0.1704 + 0.4885i -2.2107 + 0.0000i 0.4426 + 0.7962i -1.6237 + 0.0539i 2.4312 + 0.0000i -0.8262 - 0.2553i
real(M_test)
ans = 2×10
-1.2724 -1.2251 1.7022 0.2787 -0.5009 0.8866 1.7667 -0.4084 0.6188 -0.4277 0.8594 1.7922 1.6085 1.1438 0.1704 -2.2107 0.4426 -1.6237 2.4312 -0.8262
imag(M_test)
ans = 2×10
0 0 0 0 0 0 0 0 0 0 1.1664 -0.8161 0 -0.8123 0.4885 0 0.7962 0.0539 0 -0.2553
max(abs(M(:)-M_test(:))) % Good up to the precision written to file
ans = 4.3168e-06
  1 Comment
Hernia Baby
Hernia Baby on 30 Jan 2022
As you say, your answer is also good thing for us, and so you should leave it.
I really like your one and sure that this will be useful for other questions.
Thanks for your interesting and good answer! Well done!

Sign in to comment.


Atsushi Ueno
Atsushi Ueno on 29 Jan 2022
N = 3; X = rand(1,N); % 1×100のdouble配列の変数X (100は長いので3にした)
Y = rand(1,N) + i*rand(1,N); % 1×100のcomplex double配列の変数Y
Data = cat(1, X, Y) % cat関数を用いて配列変数"Data"に結合して格納
Data =
0.2828 + 0.0000i 0.3302 + 0.0000i 0.0888 + 0.0000i 0.5866 + 0.2874i 0.9533 + 0.9973i 0.4652 + 0.8897i
writematrix(Data,'Data.csv'); % writematrix関数でcsvファイルに出力
type Data.csv % 結合時に変数Dataの形式が変数Yの形式に引っ張られる
0.282768128469453+0i,0.330211111105682+0i,0.0887734539772342+0i 0.586578672060058+0.287438213218774i,0.953344286536082+0.997331817058461i,0.465180069267154+0.889695795766578i
行列ではなくセル配列を使えば、ことなる形式が共存する形でファイル出力できます。
CellData = {X; Y} % cell関数を用いてセル配列変数"CellData"に結合して格納
CellData = 2×1 cell array
{[ 0.2828 0.3302 0.0888]} {[0.5866 + 0.2874i 0.9533 + 0.9973i 0.4652 + 0.8897i]}
writecell(CellData, 'CellData.csv'); % writecell関数でことなる形式が共存する形でcsvファイルに出力
type CellData.csv % Xのデータから虚数の表示が無くなった
0.282768128469453,0.330211111105682,0.0887734539772342 0.586578672060058+0.287438213218774i,0.953344286536082+0.997331817058461i,0.465180069267154+0.889695795766578i

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!