How to check the existance of a struct field at a given index?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mr M.
el 16 de Oct. de 2018
Comentada: madhan ravi
el 16 de Oct. de 2018
p(1).name = 'name1'; p(1).value = 1:10; p(2).name = 'name2';
I want to check existance of p(2).value, but exist and empty is not working for me. How to do?
3 comentarios
Stephen23
el 16 de Oct. de 2018
Editada: Stephen23
el 16 de Oct. de 2018
"I want to check existance of p(2).value, but exist and empty is not working for me. How to do?"
Every element of a structure array has the same fields. So if you have defined this:
p(1).name = 'name1';
p(1).value = 1:10;
p(2).name = 'name2';
Then by definition of a non-scalar structure the field p(2).value also exists. If you have not allocated it a value, then it will be initialized to the empty array. This means it is not clear what your question relates to:
- p(2).value exists because it was created as soon as you created p(1).value. So testing for its existence is entirely superfluous.
- The value of that field is []. Is that what you want to test for?
Note that exist is totally irrelevant to this problem.
empty is not a suitable MATLAB function: did you mean isempty ?
Respuesta aceptada
madhan ravi
el 16 de Oct. de 2018
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!