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

📄 crone2gui.m

📁 国外人编写的一个分数阶系统的控制工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
% Hint: get(hObject,'Value') returns toggle state of radiobutton_complementbutton_state = get(hObject, 'Value');
if button_state == get(hObject, 'Max')
    set(handles.radiobutton_phase, 'Value', 0);
    edit_plant_Callback(hObject, eventdata, handles);
else
    set(handles.radiobutton_phase, 'Value', 1);
    try
        handles.phase = evalin('base', get(handles.edit_phase, 'String'));
        if any(~isreal(handles.phase)) | any(ischar(handles.phase))
            set(handles.radiobutton_phase, 'Value', 0);
            set(hObject, 'Value', 1);
            errordlg('Phases (in degrees) must be real.', 'Error with the phase', 'modal')
            edit_plant_Callback(hObject, eventdata, handles);
        end
        if size(handles.phase, 2)>1
            set(handles.radiobutton_phase, 'Value', 0);
            set(hObject, 'Value', 1);
            errordlg('The list of phases must be a column vector of real numbers.', 'Error with the phase', 'modal')
            edit_plant_Callback(hObject, eventdata, handles);
        end
    catch
        set(handles.radiobutton_phase, 'Value', 0);
        set(hObject, 'Value', 1);
        errordlg('The name you provided for the phase data variable does not seem to be valid, or else its contents are not.', 'Error with the name', 'modal')
        edit_plant_Callback(hObject, eventdata, handles);
    end
end
guidata(hObject, handles);
redraw(handles)% --- Executes during object creation, after setting all properties.function edit_order_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit_order (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction edit_order_Callback(hObject, eventdata, handles)% hObject    handle to edit_order (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit_order as text%        str2double(get(hObject,'String')) returns contents of edit_order as a doubleedit_plant_Callback(hObject, eventdata, handles);
% --- Executes during object creation, after setting all properties.function edit_phase_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit_phase (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction edit_phase_Callback(hObject, eventdata, handles)% hObject    handle to edit_phase (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit_phase as text%        str2double(get(hObject,'String')) returns contents of edit_phase as a doubleif get(handles.radiobutton_phase, 'Value') ==...
        get(handles.radiobutton_phase, 'Max')
    try
        handles.phase = evalin('base', get(handles.edit_phase, 'String'));
        if any(~isreal(handles.phase)) | any(ischar(handles.phase))
            set(handles.radiobutton_complement, 'Value', 1);
            set(handles.radiobutton_phase, 'Value', 0);
            errordlg('Phases (in degrees) must be real.', 'Error with the phase', 'modal')
            edit_plant_Callback(hObject, eventdata, handles);
        end
        if size(handles.phase, 2)>1 | length(handles.phase)~=length(handles.w)
            set(handles.radiobutton_complement, 'Value', 1);
            set(handles.radiobutton_phase, 'Value', 0);
            errordlg('The list of phases must be a column vector of real numbers with the same size of the list of frequencies.', 'Error with the phase', 'modal')
            edit_plant_Callback(hObject, eventdata, handles);
        end
    catch
        set(handles.radiobutton_complement, 'Value', 1);
        set(handles.radiobutton_phase, 'Value', 0);
        errordlg('The name you provided for the phase data variable does not seem to be valid, or else its contents are not.', 'Error with the name', 'modal')
        edit_plant_Callback(hObject, eventdata, handles);
    end
end
guidata(hObject, handles);
redraw(handles)% --- Executes during object creation, after setting all properties.function edit_plant_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit_plant (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction edit_plant_Callback(hObject, eventdata, handles)% hObject    handle to edit_plant (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit_plant as text%        str2double(get(hObject,'String')) returns contents of edit_plant as a double
try
    handles.plant = evalin('base', get(handles.edit_plant, 'String'));
    guidata(hObject, handles)
    [gain, phase] = bode(handles.plant, handles.w);
    order = str2double(get(handles.edit_order, 'String'));
    if isnan(order)
        handles.plant = tf(0);
        handles.phase = [];
        guidata(hObject, handles);
        redraw(handles);
        errordlg('Provide a valid real number for the order of the open-loop.', 'Error with the order', 'modal')
        return
    end
    handles.phase = order * 90 - squeeze(phase);
catch
    handles.plant = tf(0);
    handles.phase = [];
    set(handles.edit_plant, 'String', '');
    errordlg('Provide a valid LTI plant.', 'Error with the plant', 'modal')
end
guidata(hObject, handles)
redraw(handles);

% --- Executes during object creation, after setting all properties.function listbox1_CreateFcn(hObject, eventdata, handles)% hObject    handle to listbox1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: listbox controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end% --- Executes on button press in pushbutton_delete.function pushbutton_delete_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_delete (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)index_selected = get(handles.listbox1, 'Value');
handles.w = handles.w(setdiff(1:end, index_selected));
set(handles.listbox1, 'Value', []); % nothing may be selected in the end
set(handles.listbox1, 'String', num2str(handles.w));
% Update handles structure
guidata(hObject, handles);
% this prevents errors
if get(handles.radiobutton_Gain_data, 'Value') ==...
        get(handles.radiobutton_Gain_data, 'Max')
    set(handles.radiobutton_Gain_data, 'Value', 0)
    set(handles.radiobutton_Unit_gain, 'Value', 1)
end
if get(handles.radiobutton_phase, 'Value') ==...
        get(handles.radiobutton_phase, 'Max')
    set(handles.radiobutton_phase, 'Value', 0)
    set(handles.radiobutton_complement, 'Value', 1)
end
edit_plant_Callback(hObject, eventdata, handles);% --- Executes on button press in pushbutton_add.function pushbutton_add_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_add (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)wnew = str2double(get(handles.edit_add, 'String'));
if isnan(wnew) | ~isreal(wnew) | wnew<=0
    errordlg('Provide a valid positive real number for the new frequency.', 'Error with w', 'modal')
    return
end
handles.w = unique([handles.w; wnew]);
set(handles.listbox1, 'String', num2str(handles.w));
% Update handles structure
guidata(hObject, handles);set(handles.edit_add, 'String', ''); % this clears the field
% this prevents errors
if get(handles.radiobutton_Gain_data, 'Value') ==...
        get(handles.radiobutton_Gain_data, 'Max')
    set(handles.radiobutton_Gain_data, 'Value', 0)
    set(handles.radiobutton_Unit_gain, 'Value', 1)
end
if get(handles.radiobutton_phase, 'Value') ==...
        get(handles.radiobutton_phase, 'Max')
    set(handles.radiobutton_phase, 'Value', 0)
    set(handles.radiobutton_complement, 'Value', 1)
end
edit_plant_Callback(hObject, eventdata, handles);
% --- Executes on button press in pushbutton_load.function pushbutton_load_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton_load (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)name = get(handles.edit_load, 'String');
try
    w = evalin('base', name); % contents of variable name
    % look for complex or negative numbers and for characters
    if any(~isreal(w)) | any(w<=0) | any(ischar(w))
        errordlg('Frequencies must be real and positive.', 'Error with w', 'modal')
        return
    end
    if size(w, 2)>1
        errordlg('The list of frequencies must be a column vector of positive real numbers.', 'Error with w', 'modal')
        return
    end
    set(handles.listbox1, 'String', num2str(unique(w))); % unique also sorts
    handles.w = w;
    % Update handles structure
    guidata(hObject, handles);
    % this prevents errors
    if get(handles.radiobutton_Gain_data, 'Value') ==...
            get(handles.radiobutton_Gain_data, 'Max')
        set(handles.radiobutton_Gain_data, 'Value', 0)
        set(handles.radiobutton_Unit_gain, 'Value', 1)
    end
    if get(handles.radiobutton_phase, 'Value') ==...
            get(handles.radiobutton_phase, 'Max')
        set(handles.radiobutton_phase, 'Value', 0)
        set(handles.radiobutton_complement, 'Value', 1)
    end
    edit_plant_Callback(hObject, eventdata, handles);
catch
    errordlg('The name you provided for the frequency data variable does not seem to be valid, or else its contents are not.', 'Error with the name', 'modal')
end% --- Executes during object creation, after setting all properties.function edit_add_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit_add (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end% --- Executes during object creation, after setting all properties.function edit_load_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit_load (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end% --- Redraws the plots
function redraw(handles)
% handles     needed for handling everything

% if there are no frequencies no plot can be shown
if isempty(handles.w)
    return
end

% plotting the gain
if get(handles.radiobutton_Gain_data, 'Value') ==...
        get(handles.radiobutton_Gain_data, 'Max')
    axes(handles.axes_gain)
    semilogx(handles.w, handles.gain, '.r')
    actual = axis;
    lims = [10^(floor(log10(min(handles.w)))) 10^(ceil(log10(max(handles.w))))];
    axis([lims actual(3:4)])
    xlabel('Frequency (rad/s)')
    ylabel('Gain (dB)')
    grid
else
    axes(handles.axes_gain)
    plot(-1) % this is a stupid way of erasing the slate!
    axis([0 1 0 1])
    xlabel('Frequency (rad/s)')
    ylabel('Gain (dB)')
end

% plotting the phase
if get(handles.radiobutton_phase, 'Value') ==...
        get(handles.radiobutton_phase, 'Max')
    axes(handles.axes_phase)
    semilogx(handles.w, handles.phase, '.r')
    actual = axis;
    lims = [10^(floor(log10(min(handles.w)))) 10^(ceil(log10(max(handles.w))))];
    axis([lims actual(3:4)])
    ylabel('Phase (degrees)')
    grid
elseif dcgain(handles.plant)==0
    axes(handles.axes_phase)
    plot(-1) % this is a stupid way of erasing the slate!
    axis([0 1 0 1])
    ylabel('Phase (degrees)')
else
    try
        [gain, phase, w] = bode(handles.plant, {min(handles.w) max(handles.w)});
    catch
        [gain, phase, w] = bode(handles.plant, min(handles.w));
    end
    axes(handles.axes_phase)
    semilogx(w, squeeze(phase), handles.w, handles.phase, '.r')
    actual = axis;
    lims = [10^(floor(log10(min(handles.w)))) 10^(ceil(log10(max(handles.w))))];
    try
        axis([lims actual(3:4)])
    end
    ylabel('Phase (degrees)')
    grid
end% --- Executes during object creation, after setting all properties.function edit_n_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit_n (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end

⌨️ 快捷键说明

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