MATLAB: How to print the structure, with several fields, as a title in MATLAB

MATLAB

I have a structure with several fields. I would like to print out the value of each field as each plot's title.

Best Answer

There is no function to directly perform this in MATLAB. To do this, you will need to convert the structure to a cell array of strings and a string. The following code snippet serves as an example of how to go about this:
s.f1 = 'Sunday'; s.f2 = 'Monday'; s.f3 = 'Tuesday';
s.f4 = 'Wednesday'; s.f5 = 'Thursday'; s.f6 = 'Friday';
s.f7 = 'Saturday';
myfig = figure;
structString = [];
structString = structfun(@(field)sprintf(field),s,'UniformOutput',false);
cellstr = struct2cell(structString);
str = sprintf('\n%s',cellstr{:})
mytextbox = title(str,'FontSize','6')