Why do both writetable writematrix exist?
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jeremy Matt
el 7 de Ag. de 2023
Comentada: Voss
el 16 de Ag. de 2023
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 comentarios
Walter Roberson
el 16 de Ag. de 2023
S = ["Never"; "gonna"; "give you"; "up"; "3 o'clock"; "He said ""hello"""]
T = table(S)
filename = "test.csv";
writetable(T, filename)
dbtype(filename)
writetable(T(1:5,:), filename)
dbtype(filename)
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.
Respuesta aceptada
Voss
el 7 de Ag. de 2023
Editada: Voss
el 15 de Ag. de 2023
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Standard File Formats en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!