Azzera filtri
Azzera filtri

how to add a single blank row in a table

171 visualizzazioni (ultimi 30 giorni)
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 Commenti
Abdul Suleman
Abdul Suleman il 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 il 28 Mar 2020
Modificato: Adam Danz il 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')

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 28 Mar 2020
Modificato: Adam Danz il 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 Commento
winkmal
winkmal il 8 Gen 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).

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by