2012-12-18 2 views
4

GUI 재사용 기능 블록이 계산 정적 텍스트 상자 (s1, s2, s3s4)에 표시되어야하는 값.MATLAB이 I이 값에 두 가능한 텍스트 박스 두 가능한 텍스트 박스의 4 개 개의 스태틱 텍스트 박스 사용자 입력 값 (<code>e1</code> 및 <code>e2</code>)과 가이드에 매트랩 GUI를 만든 기반 한

e1 변경 값은 다음과 같이 될 때 값을 계산하고 e1e2

코드의 각 값에 변화를한다.

% --- Executes on key press with focus on e1 and none of its controls. 
function e1_KeyPressFcn(hObject, eventdata, handles) 
% hObject handle to e1 (see GCBO) 
% eventdata structure with the following fields (see UICONTROL) 
% Key: name of the key that was pressed, in lower case 
% Character: character interpretation of the key(s) that was pressed 
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed 
% handles structure with handles and user data (see GUIDATA) 

% Start of BLOCK 
% Get values from e1 and e2 and calculate other values 
handles.levels = str2num(get(handles.e1, 'String')); 
handles.edgelength = str2num(get(handles.e2, 'String')); 
handles.cellnum = (handles.levels^3 + 3*handles.levels^2 + 2*handles.levels)/6; 
handles.vertnum = ((handles.levels+1)^3 + 3*(handles.levels+1)^2 + 2*(handles.levels+1))/6; 

% Set values of s1, s2, s3 and s4 
set(handles.s1, 'String', num2str(handles.cellnum)); 
set(handles.s2, 'String', num2str(handles.vertnum)); 
set(handles.s3, 'String', num2str(0.433*handles.edgelength^2)); 
set(handles.s4, 'String', ... 
    num2str(2*handles.cellnum*str2num(get(handles.s3, 'String')))); 
% End of BLOCK 

function e2_KeyPressFcn뿐만 아니라 사용할 수 있도록 (BLOCK 묶인) 코드 블록을 참조 할 수 있는가? 이제 섹션을 function e2_KeyPressFcn에 붙여 넣기 만하면되지만 매우 우아하지는 않습니다.

+0

왜 당신은 당신의 새로운 기능에 모든 코드를 넣어 때 E1 또는 E2 변경 함수를 호출하지? –

답변

2

코드 블록에 도우미 기능을 만드는 것은 어떻습니까?

나는이 라인을 따라 뭔가를 생각하고 있어요 :

function e1_KeyPressFcn(hObject, eventdata, handles) 
    handles = helper_block_func(handles); 

function e2_KeyPressFcn(hObject, eventdata, handles) 
    handles = helper_block_func(handles); 

function hout = helper_block_func(hin) 
    hout = hin; 

    % # Get values from e1 and e2 and calculate other values 
    hout.levels = str2num(get(hout.e1, 'String')); 
    hout.edgelength = str2num(get(hout.e2, 'String')); 
    hout.cellnum = (hout.levels^3 + 3 * hout.levels^2 + 2 * hout.levels)/6; 
    hout.vertnum = ((hout.levels + 1)^3 + 3 * (hout.levels + 1)^2 ... 
     + 2 * (hout.levels + 1))/6 

    % # Set values of s1, s2, s3 and s4 
    set(hout.s1, 'String', num2str(hout.cellnum)); 
    set(hout.s2, 'String', num2str(hout.vertnum)); 
    set(hout.s3, 'String', num2str(0.433 * hout.edgelength^2)); 
    set(hout.s4, 'String', ... 
     num2str(2 * hout.cellnum * str2num(get(hout.s3, 'String')))); 
+0

'hout' 함수를 별도의 파일에 둘 수 있습니까? 나는 지금 시도했지만 효과가 없다. 특별한 이름을 써야 하나? –

+1

@BartArondson 헬퍼 함수'helper_block_func'를 호출했습니다 ...'hout'은 함수 이름이 아니며, 그 함수의 로컬 변수입니다. 예, 물론 별도의 m 파일에 넣을 수 있습니다. 파일의 이름이 함수처럼 지정되었는지 확인하십시오 (예 : "helper_block_func.m"). –