Speoial character in struct fieldname

20 visualizzazioni (ultimi 30 giorni)
Elzbieta
Elzbieta il 1 Dic 2024 alle 10:49
Risposto: Image Analyst il 1 Dic 2024 alle 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 Commento
Stephen23
Stephen23 il 1 Dic 2024 alle 11:05
Modificato: Stephen23 il 1 Dic 2024 alle 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.

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 1 Dic 2024 alle 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
s = struct with fields:
v0_05: 0 v0_10: 0 v0_15: 0 v0_20: 0 v0_25: 0 v0_30: 0 v0_35: 0
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
--- dictionary not found. Showing help for dictionary instead. --- dictionary - Object that maps unique keys to values Use a dictionary to efficiently lookup values associated with a key. Creation Syntax d = dictionary(keys,values) d = dictionary(k1,v1,...,kN,vN) d = dictionary Input Arguments keys - Keys scalar | array values - Values scalar | array | cell array k1,v1,...,kN,vN - Key-value pairs (as separate arguments) scalar | array Usage Syntax valueOut = d(keys) d(keys) = newValues d(keys) = [] valueOut = d{keys} d{keys} = newValues Object Functions entries - Key-value pairs of dictionary insert - Add entries to a dictionary isConfigured - Determine if dictionary has types assigned to keys and values isKey - Determine if dictionary contains key keys - Keys of dictionary lookup - Find value in dictionary by key numEntries - Number of key-value pairs in dictionary remove - Remove dictionary entries types - Types of dictionary keys and values values - Values of dictionary Examples openExample('matlab/CreateADictionaryWithSpecifiedKeysAndValuesExample') openExample('matlab/StoreHeterogeniousDataInADictionaryExample') openExample('matlab/CreateAnEmptyInitializedDictionaryExample') openExample('matlab/AddEntriesToAnUnconfiguredDictionaryExample') See also configureDictionary, insert, lookup, remove, entries, keys, values, types, numEntries, isConfigured, isKey, keyHash, keyMatch Introduced in MATLAB in R2022b Documentation for dictionary doc dictionary Folders named dictionary datatypes/dictionary io/dictionary

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by