Azzera filtri
Azzera filtri

STRUCTURE RANDOM FIELD READING FROM TEXT FILE; WHAT REPLACEMENT OF 'genvarname ' ?

2 visualizzazioni (ultimi 30 giorni)
Hello,
I have a text file containing a series of recordings (separated by a specific marker), and that each contains some fields of a structure (one structure field per line). Not all fields are present in a given recording (then those fields are assumed to have their default value); and there is no specific order in the various fields of a recording. The number of fields might also be expanded in the future, and I would like the text-data-field-reader to stay fine against any later expansion of the structure.
So the goal is to get the data from those recordings and the appropriate fields, as read from the text file.
An example of text file is given below:
%newRecord
TheStruct.field4 = 'hello';
TheStruct.fiels3 = 2.0;
%newRecord
TheStruct.field1 = 1;
TheStruct.field5 = '!';
%newRecord
(...)
What would be the simplest way to read such file, and fill the data into an array of structures TheStruct ?
An unsuccessfull try:
After reading one line at a time and identidying a new recording (%newRecord),
I went reading one line at a time (until the following %newRecord, or the end of the file)
Each new line 'NewLine' is split, eg.:
B= strtrim(split(NewLine,'=')); and then use B(1) to identify which field should be filled with B(2).
... Anyways, after having defined a structure TheStruct, and filled it with default values, and then extracted from the file say 'TheStruct.field5' :
genvarname('TheStruct.field5') gives an error
Furthermore, 'genvarname' is expected to be deprecated soon, so I would rather prefer to stay away from it.
What would be a robust yet simple solution to the problem exposed above ?
Thanks,
  1 Commento
francois heslot
francois heslot il 8 Nov 2021
in the question above, typo upon simplifying code:
I actually used :
SplitString = split(NewLine,'=');
StructureFieldName = strtrim(SplitString{1});
genvarname(StructureFieldName); % <<<< ERROR....

Accedi per commentare.

Risposta accettata

Steven Lord
Steven Lord il 8 Nov 2021
Use matlab.lang.makeValidName to ensure your candidate field name is a valid MATLAB identifier then use dynamic field names to set or query the fields in the struct using the (potentially modified) candidate field names.
S = struct;
invalidName = 'hocus pocus'; % field names can't have spaces
validName = matlab.lang.makeValidName(invalidName);
S.(validName) = 42
S = struct with fields:
hocusPocus: 42
  2 Commenti
francois heslot
francois heslot il 8 Nov 2021
Hello,
it does not give a usefull solution:
matlab.lang.makeValidName('TheStruct.field4');
gives : 'TheStruct_field4' , not exactly usefull in the context of accessing structure fields.
Alternately:
validName = matlab.lang.makeValidName('field4'); gives: 'field4'
Then what ? how do I access TheStruct.'validName' i.e. with a conversion from a valid name string to a valid field name ?
francois heslot
francois heslot il 8 Nov 2021
OK, my bad;
S.(validName) works indeed.
TheStruct.('field4') ; gives the content of TheStruct.field4
Thank you very much, problem solved !

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by