from Save Stateflow data into excel sheet by Rahul PN
Saves the data defined in the stateflow charts of the model into an excel file

gcsSfData.m
% This script collects the data used in stateflow charts in the currently
% selected simulink model and saves it into an excel file with the
% sheetname same as the model name. Multiple model data can be saved into a
% single excel file using the different sheetname. Also if the data values
% are loaded in the matlab base workspace, it will be listed along with the
% variable name. If the data value is not present in the workspace, the value
% field will be filled by 'Undefined'. The data listed in the excel file are
% sorted in alphabetical order of variable names.

tmpData = {};
sfr = sfroot;
mdl = sfr.find('-isa', 'Simulink.BlockDiagram', '-and', 'Name', gcs);
data = mdl.find('-isa', 'Stateflow.Data', '-and', '-not', 'Scope', 'Temporary');
if(isempty(data))
    disp('Stateflow data not present');
else
    for i = 1 : length(data)
        tmpData(i, 1) = {strtrim(data(i).Name)};
        tmpData(i, 2) = {data(i).Scope};
        %Take value from workspace
        try
            val = num2str(eval(data(i).Name));
            if(isempty(val))
                val = '';
            end
        catch
            val = 'Undefined';
        end
        tmpData(i, 3) = {val};
    end
    %Sort the data
    [tmpStr, sortIndex] = sort(lower(tmpData(:, 1)));
    tmpData = tmpData(sortIndex, :);
    [file, path] = uiputfile('*.xls', 'Save Model Data');
    if(~isnumeric(file))
        fname = [path, file];
        warning('off', 'MATLAB:xlswrite:AddSheet');
        sheetName = gcs;
        %If sheetname exceeds 31 char limit, truncate the name.
        if(length(sheetName) > 31)
            sheetName = [sheetName(1 : 28), '_', num2str(length(sheetName))];
        end
        xlswrite(fname, tmpData, sheetName);
        warning('on', 'MATLAB:xlswrite:AddSheet');
        disp('Excel file generated');
    end
end

Contact us at files@mathworks.com