www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/SpecifyFormattedTextAsStringArrayExample.m

    %% Specify Formatted Text as String Array
% Starting in R2016b, you can create string arrays with the |string|
% function. If you specify |formatSpec| as a string instead of a character
% vector, then the output of |sprintf| is a string.
%
% Specify |formatSpec| as a string. Convert data and return the
% result as a string.
formatSpec = string('The current time is: %d:%d %s');
A1 = 11;
A2 = 20;
A3 = 'a.m.';
str = sprintf(formatSpec,A1,A2,A3)

%%
% Convert input string. Input arrays that contain text either can be
% character vectors or strings.
A1 = 2;
A2 = 35;
A3 = string('p.m.');
str = sprintf(formatSpec,A1,A2,A3)