MATLAB Answers

Extracting fieldnames and values from a struct and converting to strings

18 views (last 30 days)
Jason
Jason on 17 Dec 2020
Commented: Jason on 17 Dec 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

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Dec 2020
strjoin(cellfun(@(N,V) N+"="+V, fieldnames(currentConfiguration), struct2cell(currentConfiguration), 'uniform', 0), '\t')

  3 Comments

Jason
Jason on 17 Dec 2020
Hi Walter, thanks for your reply.
I got this error
str=strjoin(cellfun(@(N,V) N+"="+V, fieldnames(currentConfiguration), ...
struct2cell(currentConfiguration), 'uniform', 0), '\t')
'Error using strjoin (line 53)
First input must be a string array or cell array of character vectors.
Error in Spectrometer2/InitialiseCamera (line 1256)
str=strjoin(cellfun(@(N,V) N+"="+V, fieldnames(currentConfiguration), ...
Walter Roberson
Walter Roberson on 17 Dec 2020
strjoin(cellfun(@(N,V) N+"="+V, fieldnames(currentConfiguration), struct2cell(currentConfiguration)), '\t')

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by