Does anyone know how to convert AMESim .data files to SImulink supported files.
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I want to convert AMESim software supported AMETable data (.data files) to Simulink Lookup tables suppoerted data. When I tried with the below code and applie to the N-D lookup table in simulink I am having the following error.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1728211/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1728211/image.png)
% -----------------------------------------------
% This script is developed to convert the AMESim Looukp table to Simulink
% supported format.
% It supports the AMESim [ 1D, {2D, .... 8D}, T1D(Multi 1D),
% T3D (Multi M1D), XY tables conversion to Simulink format
% -----------------------------------------------
% Input = "filename.data"
% Output = LUT ---> is a structure
% ---> LUT.BP.Breakpoint holds the Breakpoints data
% ---> LUT.BP.Lenth holds the number of elements in each breakpoint
% ---> LUT.BP.Table_data holds the table data
fname = uigetfile({'*.data';'*.txt'}, 'Select the .data file for conversion'); % To get the file from the user
if fname == 0
error('User not selected the file for conversion');
else
LUT = AMETable_2_Simulink(fname); % Function call to the conversion function
end
function LUT = AMETable_2_Simulink(fname)
fid = fopen(fname, "r", "ieee-le", "UTF-8");
file_data = fscanf(fid, '%c');
delimited_file_data = strsplit(file_data, {'\n'})'; % Splitting the file data by using new line delimiter
fclose(fid);
LUT_Dimns = strtrim(extractAfter(delimited_file_data{1},"format:")); % Finding the Table format type
% Declaring the empty arrays
x_ele = []; y_ele = []; z_ele = []; T_ele = [];
x1 = []; x2 = []; x3 = []; tbl = [];
x = []; y = []; z = []; tbl_data = [];
LUT = struct;
switch LUT_Dimns
case '1D'
% For 1D AMETable ---> 1D Lookup Table
for i = 2:length(delimited_file_data)
if ~isempty(delimited_file_data{i}) % Checking if it not an empty line
split_line = strsplit(strtrim(delimited_file_data{i}));
if ~isnan(str2double(split_line{1})) %
x = [x; split_line(1)]; % Breakpoints for the Lookup Table
tbl = [tbl; split_line(2)]; % Table for the Lookup Table
end
end
end
LUT.BP.Breakpoint = str2double(x)';
LUT.Table_data = str2double(tbl)';
case 'T1D'
% For T1D AMETable ---> 2D Lookup Table
for i = 2:length(delimited_file_data)
if ~isempty(delimited_file_data{i})
split_line = strsplit(strtrim(delimited_file_data{i}));
if ~isnan(str2double(split_line{1}))
if length(split_line{2}) <= 3
x2 = [x2; str2double(split_line{1})]; % Breakpoints2 for the Lookup Table
else
x1 = [x1; str2double(split_line{1})]; % Breakpoints1 for the Lookup Table
tbl = [tbl; str2double(split_line{2})]; % Table for the Lookup Table
if ~eq(length(x2), length(x1))
intrpl_y = repelem(x2(end), (length(x1) - length(x2)))';
x2 = [x2; intrpl_y]; % Breakpoints2 for the Lookup Table
end
end
end
end
end
arr = [x1, x2, tbl]; % Creating an array of all the data
a2tbl = array2table(arr, 'VariableNames', {'X_axis', 'Y_axis', 'Tabledata'}); % Converting array2table
y = sort(unique(x2));
x = sort(unique(x1));
% Sorting the data in ascending order and assing the NAN values to the missing MAPS
for k = 1:length(x)
xx = x(k);
for l = 1:length(y)
yy = y(l);
table_indxed = a2tbl(a2tbl.X_axis == xx & a2tbl.Y_axis == yy, :);
if isempty(table_indxed)
table_indxed = table(xx, yy, NaN, 'VariableNames', ...
{'X_axis', 'Y_axis', 'Tabledata'});
end
if ~exist('manpltd_tbl', 'var')
manpltd_tbl = table_indxed;
else
manpltd_tbl = [manpltd_tbl; table_indxed];
end
end
end
% Filling the missing MAPS with the nearest values of the MAPS
manpltd_tbl.Tabledata = fillmissing(manpltd_tbl.Tabledata, 'nearest');
% Asigning the Breakpoints & Table_data to the Structure
LUT.BP(1).Breakpoint = unique(manpltd_tbl.X_axis);
LUT.BP(2).Breakpoint = unique(manpltd_tbl.Y_axis);
LUT.BP(1).Lenth = length(unique(manpltd_tbl.X_axis));
LUT.BP(2).Lenth = length(unique(manpltd_tbl.Y_axis));
LUT.Table_data = reshape(manpltd_tbl.Tabledata, LUT.BP(:).Lenth);
case 'T3D'
% For T3D AMETable ---> 3D Lookup Table
for i = 2:length(delimited_file_data)
if ~isempty(delimited_file_data{i})
split_line = strsplit(strtrim(delimited_file_data{i}));
if ~isnan(str2double(split_line{1}))
if length(split_line{2}) <= 3
next_split_line = strsplit(strtrim(delimited_file_data{i + 1}));
if length(next_split_line{2}) <= 3
x3 = [x3; str2double(split_line{1})]; % Breakpoints3 for the Lookup Table
else
x2 = [x2; str2double(split_line{1})]; % Breakpoints2 for the Lookup Table
end
else
x1 = [x1; str2double(split_line{1})]; % Breakpoints1 for the Lookup Table
tbl = [tbl; str2double(split_line{2})];
if ~eq(length(x2), length(x1))
intrpl_y = repelem(x2(end), (length(x1) - length(x2)))';
x2 = [x2; intrpl_y]; % Breakpoints2 for the Lookup Table
end
if ~eq(length(x3), length(x1))
intrpl_z = repelem(x3(end), (length(x1) - length(x3)))';
x3 = [x3; intrpl_z]; % Breakpoints3 for the Lookup Table
end
end
end
end
end
arr = [x1, x2, x3, tbl]; % Creating an array of all the data
a2tbl = array2table(arr, 'VariableNames', {'X_axis', 'Y_axis', 'Z_axis', 'Tabledata'}); % Converting array2table
z = sort(unique(x3)); y = sort(unique(x2)); x = sort(unique(x1)); % Sorting of the data
% Sorting the data in ascending order and assing the NAN values to the missing MAPS
for k = 1:length(x)
xx = x(k);
for l = 1:length(y)
yy = y(l);
for m = 1:length(z)
zz = z(m);
table_indxed = a2tbl(a2tbl.X_axis == xx & a2tbl.Y_axis == yy & a2tbl.Z_axis == zz, :);
if isempty(table_indxed)
table_indxed = table(xx, yy, zz, NaN, 'VariableNames', ...
{'X_axis', 'Y_axis', 'Z_axis', 'Tabledata'});
end
if ~exist('manpltd_tbl', 'var')
manpltd_tbl = table_indxed;
else
manpltd_tbl = [manpltd_tbl; table_indxed];
end
end
end
end
% Filling the missing MAPS with the nearest values of the MAPS
manpltd_tbl.Tabledata = fillmissing(manpltd_tbl.Tabledata, 'nearest');
% nanLocations = isnan(manpltd_tbl.Tabledata);
% nanLinearIndexes = find(nanLocations);
% nonNanLinearIndexes = setdiff(1:numel(manpltd_tbl.Tabledata), nanLinearIndexes);
% Filling the missing MAPS with the nearest values of the MAPS
LUT.BP(1).Breakpoint = unique(manpltd_tbl.X_axis);
LUT.BP(2).Breakpoint = unique(manpltd_tbl.Y_axis);
LUT.BP(3).Breakpoint = unique(manpltd_tbl.Z_axis);
LUT.BP(1).Lenth = length(unique(manpltd_tbl.X_axis));
LUT.BP(2).Lenth = length(unique(manpltd_tbl.Y_axis));
LUT.BP(3).Lenth = length(unique(manpltd_tbl.Z_axis));
LUT.Table_data = reshape(manpltd_tbl.Tabledata, LUT.BP(:).Lenth);
case 'XY'
% For XY AMETable ---> 1D Lookup Table
for i = 2:length(delimited_file_data)
if ~isempty(delimited_file_data{i})
split_line = strsplit(delimited_file_data{i});
if ~isnan(str2double(split_line{2}))
x = [x; split_line{2}];
y = [y; split_line{3}];
z = [z; split_line{4}];
end
end
end
LUT.BP(1).Breakpoint = x;
LUT.BP(2).Breakpoint = y;
LUT.Table_data = z;
otherwise
% For {2D, 3D.... 8D] AMETable ---> 4D Lookup Table
LUT_Dimns = str2double(deblank(extractBefore(LUT_Dimns, 'D')));
n = 1;
for i = 2:length(delimited_file_data)
if ~isempty(delimited_file_data{i})
split_line = strsplit(strtrim(delimited_file_data{i}));
if ~isnan(str2double(split_line(~cellfun('isempty',split_line))))
if (n <= LUT_Dimns)
if ~isfield(LUT, 'BP')
k = i;
end
for j = 1:LUT_Dimns
LUT.BP(n).Lenth = str2double(split_line(~cellfun('isempty',split_line)));
split_BP = strsplit(delimited_file_data{i+LUT_Dimns});
LUT.BP(n).Breakpoint = str2double(split_BP(~cellfun('isempty',split_BP)));
break
end
n = n+1;
elseif i >= ( k + 2*LUT_Dimns)
line_table_data = str2double(split_line(~cellfun('isempty',split_line)));
tbl = [tbl; line_table_data];
end
end
end
end
for j = 1:size(tbl, 1)
if j == 1
conv_table_dta = transpose(tbl(j, :));
else
TransCol = transpose(tbl(j, :));
conv_table_dta = [conv_table_dta; TransCol];
end
end
LUT.Table_data = reshape(conv_table_dta, LUT.BP(:).Lenth);
end
end
0 comentarios
Respuestas (1)
Divyanshu
el 29 de Jul. de 2024
Editada: Divyanshu
el 29 de Jul. de 2024
Hi Sameer,
A possible reason for the error is that once the data in your breakpoints array are converted to your breakpoint data type, they are no longer evenly spaced and you may have to adjust the breakpoint data type to include enough precision so that your breakpoints remain evenly spaced.
You can use the function 'fixpt_evenspace_cleanup' to modify the breakpoints of lookup table to have even spacing. Please refer the following documentation link for more details:
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!