MATLAB の writetable関数で、既存のExcelファイルの最終列にテーブルを追記できますか
22 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
既存のExcelファイルの最終列の横に、別のテーブルデータを writetable 関数を使って追加することができますでしょうか。
'WriteMode'オプションを'append'にすることで最終行に追加できるのは確認できたのですが、列を追加する方法を見つけることができませんでした。
2 commentaires
Atsushi Ueno
le 25 Avr 2023
下に追加されちゃいますね
writetable(table([10;20;30]),'Book1.xlsx','WriteMode','append');
readtable('Book1.xlsx')
Réponse acceptée
Kojiro Saito
le 26 Avr 2023
writetableのappendでは行の末尾にデータが追加されるので、末尾の列にデータを追加するにはExcelのシート全体を上書きするか、Rangeで書き込み位置を指定することで実現できます。
3列のExcelファイルに4列目をセルD1に追加した例です。
tblBefore = readtable('data.xlsx')
Col4 = [1;2;3];
t = array2table(Col4);
writetable(t, 'data.xlsx', 'Range','D1')
tblAfter = readtable('data.xlsx')
3 commentaires
Kojiro Saito
le 27 Avr 2023
結構込み入っていますね。
例2の方法で、result.xlsxの列数をカウントしてその隣のセル番号に追記するコードサンプルを載せます。
C1 = readtable('ex.xlsx');
for col = 2:4 % '4'はサンプル数に応じて変更
Cc = C1(:,col) % ***
Cc1 = Cc(1,1);
Ccf0 = Cc - Cc1;
CcRatio = Ccf0 ./ Cc1;
Cc.Properties.VariableNames = {'#'};
Ccf0.Properties.VariableNames = {'deltaF'};
CcRatio.Properties.VariableNames = {'deltaF/F0'};
Tcol = [Cc Ccf0 CcRatio];
% 現在の列数をカウント
opts = detectImportOptions('result.xlsx');
colLength = length(opts.VariableNames); % 3 6 9 12...
% 書き初めのセル番号を作成
q = idivide(colLength, int16(26));
r = mod(colLength, 26);
if q == 0
cellPoint = 65+r; % D,E,...ZのASCIIコードの10進数番号
else
cellPoint = [65+q-1, 65+r]; % AA,AB,...AZ,BA,...,BZのASCIIコードの10進数番号
end
cellPointChar = [char(cellPoint), '1'];
% 書き初め列を指定して書き込み
writetable(Tcol, 'result.xlsx', 'Range', cellPointChar)
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur ワークスペース変数と MAT ファイル dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!