Speoial character in struct fieldname
20 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 1 Dec 2024 at 11:05
Edited: Stephen23
on 1 Dec 2024 at 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.
Answers (1)
Image Analyst
on 1 Dec 2024 at 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 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!