Save variables (vector) to a tabulated text file

2 ビュー (過去 30 日間)
Bill White
Bill White 2024 年 9 月 13 日
コメント済み: Ive J 2024 年 9 月 13 日
I have several vectors of data in my workspace, say A,B,C,D.....
Is it possible to save / export these vectors in a single tabulated text file that can be opened in a spreadsheet (such as excel) in columns?
I would like the colums to have headers, with the name of the vector (A, B, C, D etc.), and if possible, the file to have a name that I can choose.
regards
W

採用された回答

Ive J
Ive J 2024 年 9 月 13 日
A = randn(10, 1); B = randn(10, 1);
tab = array2table([A, B], VariableNames=["A", "B"]); % as a table
writetable(tab, "mytab.xlsx") % write to an Excel file
  2 件のコメント
Steven Lord
Steven Lord 2024 年 9 月 13 日
You're assuming that A and B have types that are compatible for concatenation. Instead I'd simply call table rather than concatenating and calling array2table. That doesn't assume or require that the types be concatenatable.
A = randn(5, 1);
B = randn(5, 1);
dt = datetime(2024, 9, randi(30, 5, 1));
tab1 = table(A, B, dt)
tab1 = 5x3 table
A B dt ________ ________ ___________ -0.83673 -0.15659 24-Sep-2024 0.40262 0.72619 08-Sep-2024 1.0433 -0.65305 03-Sep-2024 -0.51102 0.44775 01-Sep-2024 -1.5063 -0.46296 11-Sep-2024
tab2 = array2table([A, B, dt], VariableNames = ["A", "B", "dt"]) % won't work
Error using datetime/horzcat (line 715)
All inputs must be datetimes or date/time character vectors or date/time strings.
Ive J
Ive J 2024 年 9 月 13 日
@Steven Lord fair point!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by