How can I remove empty cells from struct data?
35 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
How can I remove empty cells from struct data?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1346759/image.png)
I have tried to use this but it didn't work.
Charge(Charge==0) = [];
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1346764/image.png)
2 comentarios
Oguz Kaan Hancioglu
el 5 de Abr. de 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
Respuesta aceptada
Jon
el 5 de Abr. de 2023
You can adapt @Stephen23's approach given in https://www.mathworks.com/matlabcentral/answers/613956-how-to-check-if-a-element-of-a-struct-is-empty
fun = @(s) all(structfun(@isempty,s));
idx = arrayfun(fun,Charge)
Charge(idx)=[]; % remove the empty elements
3 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!