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

    %% Share Code and Data Across Locales
%% Write Locale-Independent Date and Time Code 
% Follow these best practices when sharing code that handles dates and time with MATLAB(R) users in
% other locales. These practices ensure that the same code produces the same output
% display and that output files containing dates and time are read correctly
% on systems in different countries or with different language settings.
%%
% Create language-independent datetime values. That is, create datetime values
% that use month numbers rather than month names, such as 01 instead of
% January. Avoid using day of week names.
%%
% For example, do this:
t = datetime('today','Format','yyyy-MM-dd')
%%
% instead of this:
t = datetime('today','Format','eeee, dd-MMM-yyyy')
%%
% Display the hour using 24-hour clock notation rather than 12-hour clock
% notation. Use the |'HH'| identifiers when specifying the display format
% for datetime values.
%%
% For example, do this:
t = datetime('now','Format','HH:mm')
%%
% instead of this:
t = datetime('now','Format','hh:mm a')
%%
% When specifying the display format for time zone information, use the |Z|
% or |X| identifiers instead of the lowercase |z| to avoid the creation of time
% zone names that might not be recognized in other languages or regions.
%%
% Assign a time zone to |t|.
t.TimeZone = 'America/New_York';
%%
% Specify a language-independent display format that includes a time zone.
t.Format = 'dd-MM-yyyy Z'
%%
% If you share files but not code, you do not need to write locale-independent
% code while you work in MATLAB. However, when you write to a file, ensure
% that any text representing dates and times is language-independent. Then, other MATLAB
% users can read the files easily without having to specify a locale in
% which to interpret date and time data.
%% Write Dates in Other Languages
% Specify an appropriate format for text representing dates and times when you 
% use the |char| or |cellstr| functions. For example, convert two datetime values 
% to a cell array of character vectors using |cellstr|. Specify the format and the 
% locale to represent the day, month, and year of each datetime value as text.
t = [datetime('today');datetime('tomorrow')]
%%
S = cellstr(t,'dd. MMMM yyyy','de_DE')
%%
% |S| is a cell array of character vectors representing dates in German. You can 
% export |S| to a text file to use with systems in the |de_DE| locale.