Effacer les filtres
Effacer les filtres

how to add a single blank row in a table

171 vues (au cours des 30 derniers jours)
Abdul Suleman
Abdul Suleman le 28 Mar 2020
Commenté : winkmal le 8 Jan 2024
Dear collegues,
Supose my table is as follows
1 2 3
4 5 6
7 8 9
I want to convert it in:
1 2 3
4 5 6
7 8 9
That is, adding a blank row between some table rows. Can some one help me in this issue?
Thank you for you time.
Best,
Abdul
  2 commentaires
Abdul Suleman
Abdul Suleman le 28 Mar 2020
Thanks Adam for you help. In fact your answer helps solve my problem only partially. I would like to have " " (white space) instead of NaN. Since I am saving my table as an excel file, it would be easy to replace NaN by " " in Excel environment. Best. Abdul
Adam Danz
Adam Danz le 28 Mar 2020
Modifié(e) : Adam Danz le 29 Mar 2020
Bad news: In Matlab, numeric arrays cannot have empty values as you describe them. Numeric empty values are represented as NaN.
Good news: However, if you use writematrix, the NaN values will be written as empty values in Excel which completes you query :)
M = magic(4);
M([2 5 10]) = nan; %insert NaNs
writematrix(M,'myExcelFile.xlsx')

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 28 Mar 2020
Modifié(e) : Adam Danz le 31 Mar 2020
Note that "missing" values are represented by NaN values for numeric data.
% Create demo table
T = array2table(reshape(1:9,3,3));
% Which row should contain the empty values?
rowNum = 3;
% Create new table with row of empty values
Tnew = T([1:rowNum,rowNum:end], :);
Tnew{rowNum,:} = missing;
Result:
% Original table
T =
3×3 table
Var1 Var2 Var3
____ ____ ____
1 4 7
2 5 8
3 6 9
% Updated table
Tnew =
4×3 table
Var1 Var2 Var3
____ ____ ____
1 4 7
2 5 8
NaN NaN NaN
3 6 9
  1 commentaire
winkmal
winkmal le 8 Jan 2024
Your solution works like a charm, even for a timetable. Though this can produce a duplicate timestamp (which it does in my case, but this can be fixed afterwards).

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables 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!

Translated by