実数配列と複素数配列を結合してcsv保存する方法
9 views (last 30 days)
Show older comments
単純な質問でしたらすみません
2Dのとある解析で1×100のdouble配列の変数Xと1×100のcomplex double配列の変数Yをcat関数を用いて配列変数"Data"に結合して格納したのち、writematrix関数でcsvファイルに出力しました。
格納したデータをみるとXのデータ列が"100+0i"のように虚部がゼロの複素数形式で格納されてしまうのですが、
そのcsvデータはXのデータ列が実数表記、Yのデータ列が虚数表記という形が望ましく、やり方がわかりません。
結合時に変数Dataの形式が変数Yの形式に引っ張られるのはわかるのですが、どのようにしたらことなる形式が共存する形でファイル出力できますでしょうか。
0 Comments
Accepted Answer
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)
% writetable(A,'Sample.csv');
この場合だとcsvの最初に X, Yが入ります。
それが嫌な場合はcell型にします
B = table2cell(A)
% writecell(B,'Sample2.csv');
More Answers (2)
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.'));
% Make sure readmatrix can read it ok:
M_test = readmatrix('XY.csv')
real(M_test)
imag(M_test)
max(abs(M(:)-M_test(:))) % Good up to the precision written to file
1 Comment
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!
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"に結合して格納
writematrix(Data,'Data.csv'); % writematrix関数でcsvファイルに出力
type Data.csv % 結合時に変数Dataの形式が変数Yの形式に引っ張られる
行列ではなくセル配列を使えば、ことなる形式が共存する形でファイル出力できます。
CellData = {X; Y} % cell関数を用いてセル配列変数"CellData"に結合して格納
writecell(CellData, 'CellData.csv'); % writecell関数でことなる形式が共存する形でcsvファイルに出力
type CellData.csv % Xのデータから虚数の表示が無くなった
0 Comments
See Also
Categories
Find more on テキスト ファイル in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!