⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculatestatusequation.m

📁 matlab 应用的经典例题
💻 M
📖 第 1 页 / 共 2 页
字号:

% get the position of buttons .
ButtonXPos = TextPosition(1) + TextPosition(3) + TabSpace ;
ButtonYPos = TextPosition(2) + 5 ;
ButtonWidth = 70 ;
ButtonPosition = [ButtonXPos  ButtonYPos  ButtonWidth  TextHeight] ;

% generate the button .
ButtonHandle = uicontrol(FigureHandle, 'style', 'pushbutton', 'Units','pixels',...
    'position', ButtonPosition, 'String', ButtonString{1}, ...
    'Tag', ButtonTag{1}, 'FontSize',11, 'Callback', ButtonCallback{1} );





% define the parameters of the buttons .
ButtonString = { '上一步'; '导入'; '保存'; '下一步' } ; 
ButtonTag = { 'PreviousButton'; 'LoadButton'; 'SaveButton'; 'NextButton' } ;
ButtonCallback = { ['CalculateStatusEquation(''PreviousButton_Callback'',gcbo)']; ...
        ['CalculateStatusEquation(''LoadData_Callback'',gcbo)']; ...
        ['CalculateStatusEquation(''SaveData_Callback'',gcbo)']; ...
        ['CalculateStatusEquation(''NextButton_Callback'',gcbo)'] } ;

ButtonWidth = 80 ;
ButtonHeight = 25 ;
ButtonXPos = FigureWidth - ButtonWidth * 4 - TabSpace * 6 ;
ButtonYPos = TabSpace * 2 ;;
ButtonPosition = [ButtonXPos  ButtonYPos  ButtonWidth  ButtonHeight] ;
for num = 1: 4
    TempButtonPosition = ButtonPosition ;
    TempButtonPosition(1) = TempButtonPosition(1) + (num - 1) * (ButtonWidth + TabSpace) ;
    % generate the pushbutton: OK .
    ButtonHandle = uicontrol( 'Parent', FigureHandle, 'Units', 'Pixels', 'Position', TempButtonPosition, ...
        'Style', 'pushbutton', 'Tag',ButtonTag{num}, 'string', ButtonString{num}, 'Fontsize',12, ...
        'Callback', ButtonCallback{num} ) ;
    
end


handles = guihandles( FigureHandle ) ;
guidata( FigureHandle, handles ) ;




% --------------------------------------------------------------------------
function  init_FigureContent( h )
       
handles = guidata( h ) ;


% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;
if isempty( ArcSimulationData )
    ArcSimulationData = ArcSimulationSoft( 'Get_DefaultArcSimulationData' ) ;    
    % save the data .
    setappdata( handles.CalculateStatusEquation, 'ArcSimulationData', ArcSimulationData ) ;
end

% get the index of the element .
ElementNumber = length( ArcSimulationData.ElementParameter ) ;

% get the string of the element .
ListboxString = num2str( [1: ElementNumber]' ) ;
set( handles.ElementIndex, 'Value', 1, 'String', ListboxString ) ;

% 显示第一个元件的的参数。
ElementIndex_Callback( h ) ;

% CalculateEquation_Callback( h ) ;


% --------------------------------------------------------------------------
function  ElementIndex_Callback( h )
       
handles = guidata( h ) ;


% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;

% get the index of the element .
ElementIndex = get( handles.ElementIndex, 'Value' ) ;


% define the string of the MainCircuitParameterText .
ParameterName = { 'Type'; 'SpurTrackNumber'; 'BeginNodeIndex'; 'EndNodeIndex'; 'Parameter' } ;


% 显示当前元件的类型 .
ParameterValue = getfield( ArcSimulationData.ElementParameter, {ElementIndex}, ParameterName{1} ) ;
PopupmenuIndex = find( strcmp( ParameterValue, { '电阻'; '电感'; '电容'; '电压源' } ) ) ;
set( handles.ElementParameterEditA, 'Value', PopupmenuIndex ) ;    


TagIndex = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;

% 显示当前元件的参数 .
for num = 2: 5
    % get the handle of the edit .
    EditHandle = getfield( handles, ['ElementParameterEdit', TagIndex(num)] ) ;
    % reset the value of the edit .
    ParameterValue = getfield( ArcSimulationData.ElementParameter, {ElementIndex}, ParameterName{num} ) ;
    set( EditHandle, 'String', num2str( ParameterValue ) ) ;    
    
end




% --------------------------------------------------------------------------
function  AddElement_Callback( h )
       
handles = guidata( h ) ;

% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;

% get the index of the element .
ElementNumber = length( ArcSimulationData.ElementParameter ) ;

if ElementNumber == 30
    
    warndlg( '最多只能输入30个元件。', '警告', 'modal' ) ;
    return ;
end

% 添加新的元件。
ArcSimulationData.ElementParameter(ElementNumber + 1).Type = '电阻' ;   % 元件类型。
ArcSimulationData.ElementParameter(ElementNumber + 1).SpurTrackNumber = 1 ;   % 定义支路数。
ArcSimulationData.ElementParameter(ElementNumber + 1).BeginNodeIndex = 1 ;   % 始节点序号。
ArcSimulationData.ElementParameter(ElementNumber + 1).EndNodeIndex = 2 ;   % 终节点序号。
ArcSimulationData.ElementParameter(ElementNumber + 1).Parameter = 0.5 ;   % 定义参数值。

% save the data .
setappdata( handles.CalculateStatusEquation, 'ArcSimulationData', ArcSimulationData ) ;

% get the string of the element .
ListboxString = num2str( [1: (ElementNumber + 1)]' ) ;
set( handles.ElementIndex, 'Value', 1, 'String', ListboxString ) ;

% 显示最新一个元件的的参数。
set( handles.ElementIndex, 'Value', (ElementNumber + 1) ) ;

ElementIndex_Callback( h ) ;



% --------------------------------------------------------------------------
function  DeleteElement_Callback( h )
       
handles = guidata( h ) ;

% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;

% get the index of the element .
ElementNumber = length( ArcSimulationData.ElementParameter ) ;

if ElementNumber == 1
    
    warndlg( '至少要有一个元件。', '警告', 'modal' ) ;
    return ;
end

% 删除最后一个元件。
ArcSimulationData.ElementParameter = ArcSimulationData.ElementParameter( [1: (ElementNumber - 1)] ) ;

% save the data .
setappdata( handles.CalculateStatusEquation, 'ArcSimulationData', ArcSimulationData ) ;

% get the string of the element .
ListboxString = num2str( [1: (ElementNumber - 1)]' ) ;
set( handles.ElementIndex, 'Value', 1, 'String', ListboxString ) ;

% 显示最新一个元件的的参数。
set( handles.ElementIndex, 'Value', (ElementNumber - 1) ) ;

ElementIndex_Callback( h ) ;




% --------------------------------------------------------------------------
function  ElementParameter_Callback( h )
       
handles = guidata( h ) ;


% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;

% get the index of the element .
ElementIndex = get( handles.ElementIndex, 'Value' ) ;

% define the string of the ElementParameterText .
ParameterName = { 'Type'; 'SpurTrackNumber'; 'BeginNodeIndex'; 'EndNodeIndex'; 'Parameter' } ;


TagIndex = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;

% get the tag and the string of the edit .
EditTag = get( h, 'Tag' ) ;
EditIndex = findstr( TagIndex, EditTag(end) ) ;

ElementType = { '电阻'; '电感'; '电容'; '电压源' } ;
if EditIndex == 1
    % 选择元件类型。
    PopupmenuIndex = get( handles.ElementParameterEditA, 'Value' ) ;
    
    % 保存元件类型。
    ArcSimulationData.ElementParameter = setfield( ...
        ArcSimulationData.ElementParameter, {ElementIndex}, ParameterName{EditIndex}, ElementType{PopupmenuIndex} ) ;
    
    % save the data .
    setappdata( handles.CalculateStatusEquation, 'ArcSimulationData', ArcSimulationData ) ;
    
    return ;
end


EditValue = get( h, 'String' ) ;
EditValue = str2num( EditValue ) ;
if isempty( EditValue )
    warndlg( '请输入一个数字。', '警告' ) ;
    return ;
end

% 如果是编辑参数值。
if EditIndex == 5
        
    % 因为有一些元器件的参数是一样的,故修改其中一个的参数,其它元器件的参数也要相应的改变。
    if find( ElementIndex == [4  5  6] ) 
        ElementIndex = [4  5  6] ;
        
    elseif find( ElementIndex == [7  8  9] ) 
        ElementIndex = [7  8  9] ;
        
    elseif find( ElementIndex == [10  11  12] ) 
        ElementIndex = [10  11  12] ;
        
    elseif find( ElementIndex == [13  15] ) 
        ElementIndex = [13  15] ;
        
    elseif find( ElementIndex == [16  18] ) 
        ElementIndex = [16  18] ;
        
    end 
    
end


for num = 1: length( ElementIndex )    
    ArcSimulationData.ElementParameter = setfield( ...
        ArcSimulationData.ElementParameter, {ElementIndex(num)}, ParameterName{EditIndex}, EditValue ) ;
end

% save the data .
setappdata( handles.CalculateStatusEquation, 'ArcSimulationData', ArcSimulationData ) ;








% --------------------------------------------------------------------------
function  CalculateEquation_Callback( h )
% 计算状态方程。


handles = guidata( h ) ;


% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;

% ========================================
% 计算状态方程系数。

ArcSimulationData = CalculateEquation( ArcSimulationData ) ;

% ========================================

% save the data .
setappdata( handles.CalculateStatusEquation, 'ArcSimulationData', ArcSimulationData ) ;


AMatrixString = num2str( ArcSimulationData.StatusEquation.A ) ;
BMatrixString = num2str( ArcSimulationData.StatusEquation.B ) ;

% reset the string of the listboxes.
set( handles.DisplayAMatrix, 'Value', [], 'String', AMatrixString ) ;
set( handles.DisplayBMatrix, 'Value', [], 'String', BMatrixString ) ;




% --------------------------------------------------------------------------
function  DisplayEquation_Callback( h )
       
handles = guidata( h ) ;

% 重新计算一下,显示最新的A,B矩阵。
CalculateEquation_Callback( h ) ;

% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;

DisplayEquationABMatrix( ArcSimulationData ) ;



% --------------------------------------------------------------------------
function  LoadData_Callback( h )
       
handles = guidata( h ) ;


PromptString = '选择数据文件.' ;
WarningString1 = '文件格式出错。 ' ;
WarningString2 = '选择的文件不是正确的数据文件。 ' ;


[ FileName , PathName ] = uigetfile( {'*.mat','数据文件(*.mat)'}, PromptString ) ;


if FileName == 0
    return ;    
else
    
    [TempPathName, FileName, FileTypeName, Version] = fileparts( FileName ) ;

    if ~strcmp( FileTypeName, '.mat' ) ;
        warndlg( WarningString1, '警告', 'modal' ) ;
        return ;
    end
    SaveFile = fullfile( PathName, [FileName, FileTypeName, Version] ) ;
end

ArcSimulationData = [] ;
try
    load( SaveFile, 'ArcSimulationData' ) ;
catch
end

if isempty( ArcSimulationData ) | ~isstruct( ArcSimulationData ) ...
        | ~isfield( ArcSimulationData, 'ElementParameter' ) ...
        | ~isfield( ArcSimulationData, 'MainCircuitParameter' ) ...
        | ~isfield( ArcSimulationData, 'ArcResistanceParameter' ) 
    
    warndlg( WarningString1, '警告', 'modal' ) ;
    return ;
end


% save the data .
setappdata( handles.CalculateStatusEquation, 'ArcSimulationData', ArcSimulationData ) ;

% display the new data .
init_FigureContent( h ) ;

% reset the string of the listboxes.
set( handles.DisplayAMatrix, 'Value', [], 'String', ' ' ) ;
set( handles.DisplayBMatrix, 'Value', [], 'String', ' ' ) ;





% --------------------------------------------------------------------------
function  SaveData_Callback( h )
       
handles = guidata( h ) ;

% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;


PromptString = '保存数据文件.' ;
MessageString = '保存数据文件成功。 ' ;

[ FileName , PathName ] = uiputfile( {'*.mat','数据文件(*.mat)'}, PromptString ) ;


if FileName == 0
    return ;    
else
    
    [TempPathName, FileName, FileTypeName, Version] = fileparts( FileName ) ;

    if ~strcmp( FileTypeName, '.mat' ) ;
        FileTypeName = '.mat' ;
    else
    end
    SaveFile = fullfile( PathName, [FileName, FileTypeName, Version] ) ;
end


if strcmp( lower( FileName ), 'imagedata' ) ;
    warndlg( '不能覆盖系统文件:ImageData.mat。', '警告', 'modal' ) ;
    return ;    
end


% save the result  file .
save( SaveFile, 'ArcSimulationData' ) ;


msgbox( MessageString ) ;




% --------------------------------------------------------------------------
function  PreviousButton_Callback( h )
       
handles = guidata( h ) ;

% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;

% return the wizard figure .
ArcSimulationSoft( ArcSimulationData ) ;


% close the current figure .
pause(0) ;
close( handles.CalculateStatusEquation ) ;


% --------------------------------------------------------------------------
function  NextButton_Callback( h )
       
handles = guidata( h ) ;

% get the ArcSimulationData .
ArcSimulationData = getappdata( handles.CalculateStatusEquation, 'ArcSimulationData' ) ;

% open the next figure .
% SimulateFigure( ArcSimulationData ) ;
EditParameter( ArcSimulationData ) ;


% close the current figure .
pause(0) ;
close( handles.CalculateStatusEquation ) ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -