Extracting fieldnames and values from a struct and converting to strings

2 vues (au cours des 30 derniers jours)
Jason
Jason le 17 Déc 2020
Commenté : Jason le 17 Déc 2020
Hello.
I have a struct containing information from a camera configuration (using imaq)
currentConfiguration =
struct with fields:
TriggerType: 'manual'
TriggerCondition: 'none'
TriggerSource: 'none'
I would like to append all the fields to a UITextArea hence need to extract all the fields as strings. I have tried the following:
str=sprintf('%s\t', currentConfiguration(1:end));
ReportMessage(app,str); %This is my function to append to a UITextArea
But I get the message
'Error using sprintf
Function is not defined for 'struct' inputs.
How do I get the field name and its value all as one string, and for all fields.
thanks
Jason

Réponse acceptée

Walter Roberson
Walter Roberson le 17 Déc 2020
strjoin(cellfun(@(N,V) N+"="+V, fieldnames(currentConfiguration), struct2cell(currentConfiguration), 'uniform', 0), '\t')
  3 commentaires
Walter Roberson
Walter Roberson le 17 Déc 2020
strjoin(cellfun(@(N,V) N+"="+V, fieldnames(currentConfiguration), struct2cell(currentConfiguration)), '\t')
Jason
Jason le 17 Déc 2020
perfect, thankyou

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by