1 Convert and Concatenate Strings Arrays with Numbers and Number Arrays with Strings

Go to the MLX, M, PDF, or HTML version of this file. Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).

1.1 Generate a String from Numeric Array and Print on Screen with Counter

Want to quickly print on screen an array of number with the array name and a counter printed before each value.

% An array of numbers to print
rng(123);
ar_fl_values = rand(5,1);
% A string array of counters with leading zeros for even display
ar_st_counter_leading0 = num2str((1:length(ar_fl))', '%0.3d');
% A float array of value formatted
ar_fl_values_formated = num2str(ar_fl', '%3.2f');
ar_ch_values_display = strcat('fl_val, c', ar_st_counter_leading0, '=', ar_fl_values_formated);
% print
disp(ar_ch_values_display);

fl_val, c001=0.78
fl_val, c002=0.29
fl_val, c003=0.31
fl_val, c004=0.67
fl_val, c005=0.11
fl_val, c006=0.66
fl_val, c007=0.89
fl_val, c008=0.70
fl_val, c009=0.44
fl_val, c010=0.44

1.2 Combine A String Array with a Numeric Array using Compose

String array and numeric array, combine together using the compose function, and test different formatting functions. Formating with leading empty spaces, leading zeros, and convert to integer or not.

st_titles = ["%.3f",   "%.1f",   "%.0f";...
             "%6.3f",  "%6.1f",  "%6.0f";...
             "%06.3f", "%06.1f", "%06.0f"];
ar_params = 123.4567890 + zeros(3,3);
st_combo = compose(st_titles, ar_params);
disp(st_combo);

    "123.457"    "123.5"     "123"   
    "123.457"    " 123.5"    "   123"
    "123.457"    "0123.5"    "000123"

A string array and a numeric array combined

ls_st_param_esti = {'ar_mu_pos_1', 'ar_COEF_U_gamma'};
ar_params = [1213,456];
st_combo = strcat(ls_st_param_esti', '=', num2str(ar_params'));
disp(st_combo);

    {'ar_mu_pos_1=1213'    }
    {'ar_COEF_U_gamma= 456'}

1.3 Numeric Array to String Array with Decimal Formatting

There is a numeric array, format with a certain number of decimal points, and convert to string array.

% Inputs
rng(123);
ar_params = [1.23324, 493.1232, 4994.1111, 123];
st_rounding = '.3f';
% Rounding and to string array
ar_st_params = compose(strcat("%", st_rounding), ar_params);
% Display:
disp(ar_st_params);

    "1.233"    "493.123"    "4994.111"    "123.000"

1.4 Title from an Array of Values

There is a vector of parameter values and a vector of names for these parameter values, I want to include these in the title of a figure with the same decimal formating.

% Inputs
rng(123);
ar_params = rand(1,3);
ar_st_parms_names = ["param1", "param2", "param3"];
st_rounding = '.2f';
st_title_main = "this is the figure title";
% Rounding and combining
ar_st_params = strcat(ar_st_parms_names, compose(strcat("=%", st_rounding), ar_params));
% Generate a Single String that is comma separated:
st_param_pasted = strjoin(ar_st_params, ', ');
% Generate title with parameters
st_title_wth_params = strcat(st_title_main, ' (', st_param_pasted, ')');
% Display:
disp(st_title_wth_params);

this is the figure title (param1=0.70, param2=0.29, param3=0.23)

1.5 Combine String with Numeric Array

Example 1:

ar_fl_abc1 = [0.4 0.1 0.25 0.3 0.4];
disp([num2str(ar_fl_abc1', 'zw=%3.2f;'), num2str(ar_fl_abc1', 'zr=%3.2f')]);

zw=0.40;zr=0.40
zw=0.10;zr=0.10
zw=0.25;zr=0.25
zw=0.30;zr=0.30
zw=0.40;zr=0.40

Example 2:

close all;

rng(123);
ar_z_r_borr_mesh_wage = rand([1,5]);
ar_z_wage_mesh_r_borr = rand([1,5]);
ar_it_rows = round(rand([1,5])*10);
cl_st_full_rowscols = cellstr([num2str(ar_z_r_borr_mesh_wage', 'zr=%3.2f;'), ...
                               num2str(ar_z_wage_mesh_r_borr', 'zw=%3.2f')]);
cl_col_names = strcat('zi=', num2str(ar_it_rows([1,3,5])'), ':', cl_st_full_rowscols([1,3,5]));
disp(ar_z_r_borr_mesh_wage);

    0.6965    0.2861    0.2269    0.5513    0.7195

disp(ar_z_wage_mesh_r_borr);

    0.4231    0.9808    0.6848    0.4809    0.3921

disp(cl_st_full_rowscols);

    {'zr=0.70;zw=0.42'}
    {'zr=0.29;zw=0.98'}
    {'zr=0.23;zw=0.68'}
    {'zr=0.55;zw=0.48'}
    {'zr=0.72;zw=0.39'}

disp(cl_col_names);

    {'zi=3:zr=0.70;zw=0.42'}
    {'zi=4:zr=0.23;zw=0.68'}
    {'zi=4:zr=0.72;zw=0.39'}

1.6 Combine Number with String Cell Array

We have a string cell array we created from the previous section, now append numbers to it

% Append Common Numbers
cl_col_names_append = strcat(cl_col_names, '-String-Cell-With-Numeric-', num2str(123));
disp(cl_col_names_append);

    {'zi=3:zr=0.70;zw=0.42-String-Cell-With-Numeric-123'}
    {'zi=4:zr=0.23;zw=0.68-String-Cell-With-Numeric-123'}
    {'zi=4:zr=0.72;zw=0.39-String-Cell-With-Numeric-123'}

1.7 Combine Numeric Array with String Cell Array

Append an array of numeric values

% Append Common Numbers
cl_col_names_append = strcat(cl_col_names, '-String-Cell-With-Numeric-Array-', ...
    num2str(transpose(1:length(cl_col_names))));
disp(cl_col_names_append);

    {'zi=3:zr=0.70;zw=0.42-String-Cell-With-Numeric-Array-1'}
    {'zi=4:zr=0.23;zw=0.68-String-Cell-With-Numeric-Array-2'}
    {'zi=4:zr=0.72;zw=0.39-String-Cell-With-Numeric-Array-3'}

1.8 Convert Numeric Array to String, Apeend Prefix to all elements.

ar_fl_abc1 = [0.4 0.1 0.25 0.3 0.4];
ar_st_wth_prefix = strcat('row=', string(ar_fl_abc1));
disp(ar_st_wth_prefix);

    "row=0.4"    "row=0.1"    "row=0.25"    "row=0.3"    "row=0.4"


% Does Array Exist in Longer Array as Subset
ar_abc1 = [0.4 0.1 0.25 0.3 0.4];
ar_abc2 = [0.4 0.1 0.2 0.3 0.4];
ar_efg = [0.1 0.2 0.3 0.4 0.1 0.2 0.3 0.4 0.1 0.2 0.3 0.4 0.1 0.2 0.3 0.4];
st_abc1 = strjoin(string(num2str(ar_abc1)));
st_abc2 = strjoin(string(num2str(ar_abc2)));
st_efg = strjoin(string(num2str(ar_efg)));
contains(st_efg, st_abc1)

ans = 
   0

contains(st_efg, st_abc2)

ans = 
   1


% Display Convert to String
fprintf('Display string [%s]', num2str([1,2,3]));

Display string [1  2  3]

fprintf('Display string [%s]', num2str(1.1));

Display string [1.1]

fprintf('Display string [%s]', 'abc');

Display string [abc]