Speoial character in struct fieldname
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Stephen23
am 1 Dez. 2024
Bearbeitet: Stephen23
am 1 Dez. 2024
"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.
Antworten (1)
Image Analyst
am 1 Dez. 2024
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 Kommentare
Siehe auch
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!