Effacer les filtres
Effacer les filtres

How do I convert a field name into a variable name in a script?

3 vues (au cours des 30 derniers jours)
Alexander
Alexander le 22 Nov 2023
Modifié(e) : Stephen23 le 22 Nov 2023
I've loaded a matfile structure with unknown fields and want to write a script that processes the data in the fields.
>> S = load('matfile.mat');
>> fields = fieldnames(S)
fields =
1×1 cell array
{'InterestingStuff'}
InterestingStuff is an array of doubles. To access it not knowing the field name,
fields{1}(1,1)
for instance doesn't work, neither does
string(fields{1})(1,1)
What can I use in a script?
  1 commentaire
Stephen23
Stephen23 le 22 Nov 2023
Modifié(e) : Stephen23 le 22 Nov 2023
So you are using a structure but don't know how to define the fieldname dynamically... a good place to start is to simply open the STRUCT documentation:
and then scroll to the bottom where you will find lists of related functions and articles, like this one:

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 22 Nov 2023
S.(fields{1})
That is, structure (or object) name, then "." then ( then an expression that evaluates to a character vector or string scalar, then )

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by