How to fill a blank cell with the value of the cell above it in a table?

5 ビュー (過去 30 日間)
KD
KD 2025 年 3 月 24 日
コメント済み: KD 2025 年 3 月 24 日
Hello,
I am running a script that reads through a large excel file and creates a table. One of the columns in the file will sometimes have blank cells. The value should be the value that is above it, but this sheet is large and updated often, so it would be too much to manually update it each time.
This is the code I'm currently using that isn't working:
for j = 1:height(data)
%Fill in blank data
if isempty(data.Var1)
data.Var1 = fillmissing(data.Var1,'previous');
end
end
I attached the output that I'm getting. Instead of 0x0 char, it should be the value listed above.
Thank you!

採用された回答

Matt J
Matt J 2025 年 3 月 24 日
編集済み: Matt J 2025 年 3 月 24 日
The only line of code you need is this one
data.Var1=fillmissing(data.Var1,'previous')
Get rid of the lines around it.
Var1={'ABC01';'';'ddddd';''}; Var2=(1:4)';
data=table(Var1,Var2)
data = 4x2 table
Var1 Var2 __________ ____ {'ABC01' } 1 {0x0 char} 2 {'ddddd' } 3 {0x0 char} 4
data.Var1=fillmissing(data.Var1,'previous')
data = 4x2 table
Var1 Var2 _________ ____ {'ABC01'} 1 {'ABC01'} 2 {'ddddd'} 3 {'ddddd'} 4
  1 件のコメント
KD
KD 2025 年 3 月 24 日
wow one line change makes all the difference! thanks!!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2025 年 3 月 24 日
if isempty(data.Var1)
You are testing all of the Var1 component of data at the same time there, which does not make sense since you are inside a for loop.
Your code should just be
data.Var1 = fillmissing(data.Var1, 'previous');
with nothing else -- no for loop.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by