Speoial character in struct fieldname

1 Ansicht (letzte 30 Tage)
Elzbieta
Elzbieta am 1 Dez. 2024
Beantwortet: Image Analyst am 1 Dez. 2024
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
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
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
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

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!

Translated by