Files
sunny360vfe_proj/utilities/getSigfromExcel_rec.m
T
2025-06-04 14:13:22 +08:00

157 lines
6.0 KiB
Matlab

function getSigfromExcel_rec(filename,sldd_name)
% @ filename: the name of the excel to be read (e.g. '*.xlsx')
% @ sldd_name: the name of saved sldd (e.g. '*.sldd')
%% Setup the Import Options
[~,sheets,~] = xlsfinfo(filename);
SigName = [];
for k2 = 1:numel(sheets)
opts = spreadsheetImportOptions("NumVariables", 17);
% Specify sheet and range
opts.Sheet = sheets{k2};
opts.DataRange = 'A2:Q65535';
% Specify column names and types
opts.VariableNames = ["Name", "StorageClass", "Alias", "Alignment", "CustomStorageClass", "ConcurrentAccess", "StructName", "Description", "DataType", "Min","Max","DocUnits","Dimensions","DimensionsMode","Complexity","SampleTime","InitialValue"];
opts.VariableTypes = ["string", "string", "string", "string", "string", "string", "string", "string", "string", "string","string","string", "string", "string", "string", "string","string"];
opts = setvaropts(opts, [1, 17], "WhitespaceRule", "preserve");
opts = setvaropts(opts, [1, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], "EmptyFieldRule", "auto");
% Import the data
tmp = readtable(filename, opts, "UseExcel", false);
%% Clear temporary variables
clear opts
%% create bus objects
for k1 = 1:sum(tmp.Name ~= "")
SigName = [SigName, string(tmp.Name(k1))];
elems(k1) = Simulink.Signal;
% StorageClass
if strcmp(tmp.StorageClass(k1),"[]") || strcmp(tmp.StorageClass(k1),"")
elems(k1).CoderInfo.StorageClass = 'Custom';
else
elems(k1).CoderInfo.StorageClass = tmp.StorageClass(k1);
end
elems(k1).CoderInfo.Alias = tmp.Alias(k1);
% Alignment
if strcmp(tmp.Alignment(k1),"[]") || strcmp(tmp.Alignment(k1),"")
elems(k1).CoderInfo.Alignment = -1;
else
elems(k1).CoderInfo.Alignment = str2num(tmp.Alignment(k1));
end
% CustomStorageClass
if strcmp(tmp.CustomStorageClass(k1),"[]") || strcmp(tmp.CustomStorageClass(k1),"")
elems(k1).CoderInfo.CustomStorageClass = 'Struct';
else
elems(k1).CoderInfo.CustomStorageClass = tmp.CustomStorageClass(k1);
end
elems(k1).CoderInfo.CustomAttributes.ConcurrentAccess = eval(char(tmp.ConcurrentAccess(k1)));
elems(k1).CoderInfo.CustomAttributes.HeaderFile = strcat('rec.h');
elems(k1).CoderInfo.Alias = strcat(tmp.StructName(k1),'_',tmp.Name(k1));
elems(k1).Description = tmp.Description(k1);
elems(k1).DataType = tmp.DataType(k1);
% min
if strcmp(tmp.Min(k1),"[]") || strcmp(tmp.Min(k1),"")
elems(k1).Min = [];
else
elems(k1).Min = str2num(tmp.Min(k1));
end
% max
if strcmp(tmp.Max(k1),"[]") || strcmp(tmp.Min(k1),"")
elems(k1).Max = [];
else
elems(k1).Max = str2num(tmp.Max(k1));
end
elems(k1).DocUnits = tmp.DocUnits(k1);
% Dimensions
if strcmp(tmp.Dimensions(k1),"[]") || strcmp(tmp.Dimensions(k1),"")
elems(k1).Dimensions = 1;
else
elems(k1).Dimensions = str2num(tmp.Dimensions(k1));
end
% DimensionsMode
if strcmp(tmp.DimensionsMode(k1),"[]") || strcmp(tmp.DimensionsMode(k1),"")
elems(k1).DimensionsMode = 'auto';
else
elems(k1).DimensionsMode = tmp.DimensionsMode(k1);
end
% Complexity
if strcmp(tmp.Complexity(k1),"[]") || strcmp(tmp.Complexity(k1),"")
elems(k1).Complexity = 'auto';
else
elems(k1).Complexity = tmp.Complexity(k1);
end
% SampleTime
if strcmp(tmp.SampleTime(k1),"[]") || strcmp(tmp.SampleTime(k1),"")
elems(k1).SampleTime = -1;
else
elems(k1).SampleTime = str2num(tmp.SampleTime(k1));
end
elems(k1).InitialValue = tmp.InitialValue(k1);
eval([char(tmp.Name(k1)),' = ','elems(k1);']);
% output to workspace
assignin('base',char(tmp.Name(k1)),eval(char(tmp.Name(k1))));
end
end
% generate header file
structname = unique(tmp.StructName);
sn_sel = structname(structname ~= "");
num_sn = numel(sn_sel);
mdata_type = {'double','single','int8','uint8','int16','uint16','int32','uint32','boolean'};
cdata_type = {'real_T','real32_T','int8_T','uint8_T','int16_T','uint16_T','int32_T','uint32_T','boolean_T'};
fid = fopen('rec.h','w');
fprintf(fid,'#ifndef __REC_H__\n');
fprintf(fid,'#define __REC_H__\n');
for idx = 1:num_sn
sig_sel = tmp.StructName == sn_sel(idx);
num_sig = numel(tmp.Name(sig_sel));
name = tmp.Name(sig_sel);
type = tmp.DataType(sig_sel);
fprintf(fid,'\n');
fprintf(fid,'typedef struct {\n');
for idk = 1:num_sig
ind = strcmp(mdata_type,type(idk));
fprintf(fid,'\t');
fprintf(fid,strcat(cdata_type(ind),{32}, name(idk),';'));
fprintf(fid,'\n');
end
fprintf(fid,'}');
fprintf(fid,strcat(upper(sn_sel(idx)),'_t;'));
fprintf(fid,'\n');
end
fprintf(fid,'\n');
for idk = 1:num_sn
fprintf(fid,strcat('extern',{32},upper(sn_sel(idk)),'_t',{32},sn_sel(idk),';'));
fprintf(fid,'\n');
end
fprintf(fid,'\n');
for idx = 1:num_sn
sig_sel = tmp.StructName == sn_sel(idx);
num_sig = numel(tmp.Name(sig_sel));
name = tmp.Name(sig_sel);
for idk = 1:num_sig
fprintf(fid,strcat('#define',{32},sn_sel(idx),'_',name(idk),{32},...
'(',sn_sel(idx),'.',name(idk),')'));
fprintf(fid,'\n');
end
end
fprintf(fid,'\n');
fprintf(fid,'#endif');
fclose(fid);
% close all the dictionary
Simulink.data.dictionary.closeAll;
% delete existed data dictionary~
if exist(sldd_name)
eval(['delete ',sldd_name]);
end
% create sldd file
dictionaryObj = Simulink.data.dictionary.create(sldd_name);
% Import to the dictionary the model variables defined in the base
% workspace, and clear the variables from the base workspace
importFromBaseWorkspace(dictionaryObj,'varList',SigName,'clearWorkspaceVars',true);
% save dictionary
saveChanges(dictionaryObj);
end