Why do both writetable writematrix exist?

19 ビュー (過去 30 日間)
Jeremy Matt
Jeremy Matt 2023 年 8 月 7 日
コメント済み: Voss 2023 年 8 月 16 日
It seems like they have overlapping functionality and this overlap causes problems. Take this use case for example: I'm trying to detect a certain type of interval in an audio stream. If intervals are detected, I want to write the intervals to an excel sheet, other wise I want to write "no intervals detected" to the excel sheet.
The problem is that if there are intervals, the variable I'm trying to write is a table and I need to use writetable. However, if there are no intervals, the variable is a char and writetable will not write chars. Writetable also will not write string arrays. To write chars, I writematrix is required. However, writematrix can't write tables.
The solution as far as I can tell is below, but feels hacky and unecessary:
if ischar(resultTable)
writematrix(resultTable,finalpath,'Sheet','predicted_intervals');
else
writetable(resultTable,finalpath,'Sheet','predicted_intervals');
end
  2 件のコメント
Walter Roberson
Walter Roberson 2023 年 8 月 16 日
S = ["Never"; "gonna"; "give you"; "up"; "3 o'clock"; "He said ""hello"""]
S = 6×1 string array
"Never" "gonna" "give you" "up" "3 o'clock" "He said "hello""
T = table(S)
T = 6×1 table
S _________________ "Never" "gonna" "give you" "up" "3 o'clock" "He said "hello""
filename = "test.csv";
writetable(T, filename)
dbtype(filename)
1 S 2 "Never" 3 "gonna" 4 "give you" 5 "up" 6 "3 o'clock" 7 "He said ""hello"""
writetable(T(1:5,:), filename)
dbtype(filename)
1 S 2 Never 3 gonna 4 give you 5 up 6 3 o'clock
Looks to me as if it handles string arrays. Whether it quotes or not depends on the current QuoteStrings option value and on whether any entries require quoting.
Jeremy Matt
Jeremy Matt 2023 年 8 月 16 日
Ahh, I was trying the code below; I didn't convert S to a table (I missed that this function exists - I'm pretty rusty at Matlab at this point). I guess this works, I suppose you can just writetable(T(2:,:)) to skip writing the original variable name on the first line of the .csv.
Thanks.
S=["no intervals detected","another part"]
S = 1×2 string array
"no intervals detected" "another part"
filename = "test.csv";
writetable(S, filename)
Error using writetable
Unsupported type 'string'. Use writematrix instead.

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

採用された回答

Voss
Voss 2023 年 8 月 7 日
編集済み: Voss 2023 年 8 月 15 日
writetable and writematrix work for different data types, as you point out. If you want to be able to use writetable whether intervals were detected or not, you can make that char vector into a table. Something like:
% your code that creates resultTable, which
% may be a table or a char vector, goes here
if ~istable(resultTable)
resultTable = table([],'VariableNames',{resultTable});
end
writetable(resultTable,finalpath,'Sheet','predicted_intervals');
Alternatively, you can reduce the redundancy of your current set-up a bit:
% your code that creates resultTable, which
% may be a table or a char vector, goes here
if istable(resultTable)
f = @writetable;
else
f = @writematrix;
end
f(resultTable,finalpath,'Sheet','predicted_intervals')
  3 件のコメント
Jeremy Matt
Jeremy Matt 2023 年 8 月 16 日
The first approach is best in my opinion.
Voss
Voss 2023 年 8 月 16 日
I agree.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by