Speoial character in struct fieldname
20 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Elzbieta
el 1 de Dic. de 2024 a las 10:49
Respondida: Image Analyst
el 1 de Dic. de 2024 a las 16:11
Hello,
I have to create structure with fields based on measurements, namely float number:amplitudes = {'0.05', '0.10', '0.15', '0.20', '0.25', '0.30', '0.35',...
'0.40', '0.45', '0.50', '1.00', '1.50', '2.00', '2.50', '3.00',...
'3.50', '4.00', '4.50', '5.00', '5.50' }
How to pass special character "." or "_" in struct field name?
Cheers
E
1 comentario
Stephen23
el 1 de Dic. de 2024 a las 11:05
Editada: Stephen23
el 1 de Dic. de 2024 a las 15:19
"How to pass special character "." or "_" in struct field name?"
It is not possible to use "." in fieldnames: "Field names can contain ASCII letters (A–Z, a–z), digits (0–9), and underscores, and must begin with a letter". Source:
Note that rather than converting numeric data to text and then awkwardly force them into dynamic fieldnames you should just store your numeric data as numeric data: possibly a non-scalar struct would be better data design. Or a scalar struct with non-scalar fields. Indexing would be more efficient.
In any case, avoid forcing meta-data into fieldnames.
Respuestas (1)
Image Analyst
el 1 de Dic. de 2024 a las 16:11
Have them start with a letter and use underscores instead of dots.
fn = {'v0_05', 'v0_10', 'v0_15', 'v0_20', 'v0_25', 'v0_30', 'v0_35'};
for k = 1 : numel(fn)
s.(fn{k}) = 0;
end
s
Perhaps you might like to learn about dictionary, though I'd probably use @Stephen23's idea of using a structure array where you just know what index corresponds to each of your floating point values.
help dictionary
0 comentarios
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!