How can I remove empty cells from struct data?

How can I remove empty cells from struct data?
I have tried to use this but it didn't work.
Charge(Charge==0) = [];

2 comentarios

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 el 6 de Abr. de 2023
Thank you for your answer.

Iniciar sesión para comentar.

 Respuesta aceptada

Jon
Jon el 5 de Abr. de 2023
fun = @(s) all(structfun(@isempty,s));
idx = arrayfun(fun,Charge)
Charge(idx)=[]; % remove the empty elements

3 comentarios

Jon
Jon el 5 de Abr. de 2023
Editada: Jon el 5 de Abr. de 2023
Assuming that all of the fields are empty for the elements that you want to remove you could do this, which is a simpler
% find the locations where there are empty fields, assume that if
% Charge.Voltage_measured is empty the other fields are empty too
idl = cellfun('isempty',{Charge.Voltage_measured});
% just keep the non-empty elements
Charge = Charge(~idl); % or alternatively remove the empty ones using Charge(idl) = [];
Bob
Bob el 6 de Abr. de 2023
Thank you for your answer
Jon
Jon el 6 de Abr. de 2023
Your welcome

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

Bob
el 5 de Abr. de 2023

Comentada:

Jon
el 6 de Abr. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by