How can I remove empty cells from struct data?

52 Ansichten (letzte 30 Tage)
Bob
Bob am 5 Apr. 2023
Kommentiert: Jon am 6 Apr. 2023
How can I remove empty cells from struct data?
I have tried to use this but it didn't work.
Charge(Charge==0) = [];
  2 Kommentare
Oguz Kaan Hancioglu
Oguz Kaan Hancioglu am 5 Apr. 2023
You need to build for loop to check the filed of struct is empty or not. You can use this code.
k = 1;
for i = 1:length(Charge)
if ~isempty(Charge(i).Voltage_measured)
ChargeNew(k).Voltage_measured = Charge(i).Voltage_measured;
ChargeNew(k).Current_measured = Charge(i).Current_measured;
ChargeNew(k).Temperature_measured = Charge(i).Temperature_measured;
ChargeNew(k).Current_charge = Charge(i).Current_charge;
ChargeNew(k).Voltage_charge = Charge(i).Voltage_charge;
ChargeNew(k).Time = Charge(i).Time;
k = k + 1;
end
end
Bob
Bob am 6 Apr. 2023
Thank you for your answer.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jon
Jon am 5 Apr. 2023
fun = @(s) all(structfun(@isempty,s));
idx = arrayfun(fun,Charge)
Charge(idx)=[]; % remove the empty elements
  3 Kommentare
Bob
Bob am 6 Apr. 2023
Thank you for your answer
Jon
Jon am 6 Apr. 2023
Your welcome

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by