release 1.0
This commit is contained in:
157
McuLib/m/customtable.m
Normal file
157
McuLib/m/customtable.m
Normal file
@@ -0,0 +1,157 @@
|
||||
classdef customtable
|
||||
|
||||
methods(Static)
|
||||
% формирование таблицы на всю ширину
|
||||
function format(table_name)
|
||||
block = gcb;
|
||||
mask = Simulink.Mask.get(block);
|
||||
tableControl = mask.getDialogControl(table_name);
|
||||
tableParameter = mask.getParameter(table_name);
|
||||
nCols = tableControl.getNumberOfColumns;
|
||||
if nCols > 0
|
||||
for i = 1:nCols
|
||||
tableControl.removeColumn(1);
|
||||
end
|
||||
end
|
||||
column = tableControl.addColumn(Name='Title', Type='edit');
|
||||
tableControl.Sortable = 'on';
|
||||
column.Name = tableParameter.Alias;
|
||||
end
|
||||
|
||||
|
||||
function update(tableName)
|
||||
block = gcb;
|
||||
mask = Simulink.Mask.get(block);
|
||||
Table = mask.getParameter(tableName);
|
||||
|
||||
cellArray = customtable.parse(tableName);
|
||||
cleaned = customtable.removeEmptyRows(cellArray);
|
||||
|
||||
if numel(cleaned) ~= numel(cellArray)
|
||||
quoted = cellfun(@(s) ['''' s ''''], cleaned, 'UniformOutput', false);
|
||||
newStr = ['{' strjoin(quoted, ';') '}'];
|
||||
Table.Value = newStr;
|
||||
end
|
||||
end
|
||||
|
||||
function column_titles = save_all_tables(table_names)
|
||||
% Очищает столбцы в каждой таблице из массива имен table_names
|
||||
% Возвращает cell-массив с названиями первых столбцов каждой таблицы
|
||||
block = gcb;
|
||||
|
||||
% Получить объект маски блока
|
||||
maskObj = Simulink.Mask.get(block);
|
||||
|
||||
% Инициализировать cell-массив для хранения названий столбцов
|
||||
column_titles = cell(size(table_names));
|
||||
|
||||
for k = 1:numel(table_names)
|
||||
table_name = table_names{k};
|
||||
|
||||
% Получить объект управления таблицей
|
||||
tableControl = maskObj.getDialogControl(table_name);
|
||||
|
||||
% Получить количество столбцов
|
||||
nCols = tableControl.getNumberOfColumns;
|
||||
|
||||
if nCols > 0
|
||||
% Получить первый столбец (который будем удалять)
|
||||
column = tableControl.getColumn(1);
|
||||
column_titles{k} = column.Name;
|
||||
|
||||
% Удаляем все столбцы
|
||||
% Важно: при удалении столбцов индексы меняются,
|
||||
% поэтому удаляем всегда первый столбец nCols раз
|
||||
for i = 1:nCols
|
||||
tableControl.removeColumn(1);
|
||||
end
|
||||
else
|
||||
% Если столбцов нет, возвращаем пустую строку
|
||||
column_titles{k} = '';
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function restore_all_tables(table_names, column_titles)
|
||||
% Восстанавливает первый столбец в каждой таблице из массива имен
|
||||
% Использует массив column_titles для установки имени столбца
|
||||
block = gcb;
|
||||
|
||||
% Получить объект маски блока
|
||||
maskObj = Simulink.Mask.get(block);
|
||||
|
||||
for k = 1:numel(table_names)
|
||||
table_name = table_names{k};
|
||||
title = column_titles{k};
|
||||
|
||||
% Получить объект управления таблицей
|
||||
tableControl = maskObj.getDialogControl(table_name);
|
||||
|
||||
% Добавить новый столбец
|
||||
column = tableControl.addColumn(Name='title', Type='edit');
|
||||
column.Name = title;
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function out = parse(tableName)
|
||||
block = gcb;
|
||||
TableStr = get_param(block, tableName);
|
||||
out = customtable.parse__(TableStr);
|
||||
end
|
||||
|
||||
function collect(tableName, cellArray)
|
||||
block = gcb;
|
||||
newTableStr = customtable.collect__(cellArray);
|
||||
% Записываем обратно в параметр маски
|
||||
set_param(block, tableName, newTableStr);
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
methods(Static, Access=private)
|
||||
|
||||
function out = parse__(tableStr)
|
||||
str = strtrim(tableStr);
|
||||
if startsWith(str, '{') && endsWith(str, '}')
|
||||
str = str(2:end-1);
|
||||
end
|
||||
|
||||
parts = split(str, ';');
|
||||
out = cell(numel(parts), 1);
|
||||
for i = 1:numel(parts)
|
||||
el = strtrim(parts{i});
|
||||
if startsWith(el, '''') && endsWith(el, '''')
|
||||
el = el(2:end-1);
|
||||
end
|
||||
out{i} = el;
|
||||
end
|
||||
|
||||
if isempty(out) || (numel(out) == 1 && isempty(out{1}))
|
||||
out = {};
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function tableStr = collect__(cellArray)
|
||||
quoted = cellfun(@(s) ['''' s ''''], cellArray, 'UniformOutput', false);
|
||||
tableStr = ['{' strjoin(quoted, ';') '}'];
|
||||
end
|
||||
|
||||
|
||||
function cleaned = removeEmptyRows(cellArray)
|
||||
if isempty(cellArray)
|
||||
cleaned = {};
|
||||
else
|
||||
% Проверяем каждую строку, есть ли в ней содержимое (не пустая строка)
|
||||
isEmptyRow = cellfun(@(s) isempty(strtrim(s)), cellArray);
|
||||
cleaned = cellArray(~isEmptyRow);
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user