基于功能的GUI

时间:2012-08-26 17:57:18

标签: matlab user-interface matlab-guide

我刚刚编写了一个Matlab函数,现在我想通过GUI调用它(目前我还记得它是可执行的)。

我正在尝试使用GUIDE,但结果很少/很差。

我只想让GUI有:

1. One edit bar for text files;
2. One push button for running the function;
3. One push button for resetting the GUI;
4. push button for closing the GUI on user command.

功能如下:

    function kaimal(file1)

    if exist(file1, 'file')~=2
        info_text;
        return
    end

    tic

    f = fopen(file1,'rt');
    C = textscan(f, '%s', 'Delimiter', '\r\n');
    C = C{1};
    fclose(f);

    DLC = C(4:numel(C),1);
    num_dlc = numel(DLC)/3;
    DLC = reshape(DLC,3,num_dlc);

    f2 = fopen(C{3,1},'rt' );
    REF = textscan(f2, '%s', 'Delimiter', '\r\n');
    REF = REF{1};
    fclose(f2);

    REF1 = REF;



    for i=1:num_dlc
        DLC{2,i}=str2num(DLC{2,i});
        DLC{3,i}=str2num(DLC{3,i});
    end

    for l = 1:num_dlc
        fprintf('Generation of: %s\r\n', DLC{1,l})
        for j = 1:DLC{3,l}
            f1 = [C{2,1} '\' DLC{1,l}];
            if exist(f1,'dir') ~= 7
                mkdir(f1);
            end
            fname = [f1 '\' 's' num2str(j)];
            if exist(fname,'dir') ~= 7
                mkdir(fname);
            end
            for i = 1:length(DLC{2,l}),
                filename = [fname '\' num2str(DLC{2,l}(i)) '.$PJ'];
                REF1{strncmp('LENGTH', REF, length('LENGTH'))} = sprintf('LENGTH %g', DLC{2,l}(i)*610);
                REF1{strncmp('STEP', REF, length('STEP'))} = sprintf('STEP %g', DLC{2,l}(i)/13.4295);
                REF1{strncmp('UBAR', REF, length('UBAR'))} = sprintf('UBAR %g', DLC{2,l}(i));
                REF1{strncmp('SEED', REF, length('SEED'))} = sprintf('SEED %i', randi(999,1));

                fid = fopen(filename,'w');
                txt = sprintf([repmat('%s\r\n',1,size(REF1,1)),'\r\n'],REF1{:});
                fprintf(fid, '%s', txt);
                fclose(fid);
            end
        end
        toc
    end
    end

function info_text
disp('The input file does not exist: Please assign a proper input file')
end

您好,

目前我正在尝试这个,但它不起作用导致主要功能未正确读取:

function varargout = test_gui_1(varargin)
% TEST_GUI_1 M-file for test_gui_1.fig
%      TEST_GUI_1, by itself, creates a new TEST_GUI_1 or raises the existing
%      singleton*.
%
%      H = TEST_GUI_1 returns the handle to a new TEST_GUI_1 or the handle to
%      the existing singleton*.
%
%      TEST_GUI_1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST_GUI_1.M with the given input arguments.
%
%      TEST_GUI_1('Property','Value',...) creates a new TEST_GUI_1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before test_gui_1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to test_gui_1_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help test_gui_1

% Last Modified by GUIDE v2.5 27-Aug-2012 20:35:06

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test_gui_1_OpeningFcn, ...
                   'gui_OutputFcn',  @test_gui_1_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before test_gui_1 is made visible.
function test_gui_1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to test_gui_1 (see VARARGIN)

% Choose default command line output for test_gui_1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test_gui_1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = test_gui_1_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pb3_close.
function pb3_close_Callback(hObject, eventdata, handles)
% hObject    handle to pb3_close (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(handles.figure1)


% --- Executes on button press in pb1_wind.
function pb1_wind_Callback(hObject, eventdata, handles)
% hObject    handle to pb1_wind (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kaimal(handles.file1)


% --- Executes on button press in pb2_reset.
function pb2_reset_Callback(hObject, eventdata, handles)
% hObject    handle to pb2_reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit1,'String','Edit text'); 


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.file1 = (get(hObject,'String'));
guidata(hObject, handles);

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

Matlab检索的错误消息是:

??? Undefined function or method 'kaimal' for input arguments of type 'char'.

Error in ==> test_gui_1>pb1_wind_Callback at 89
kaimal(handles.file1)

Error in ==> gui_mainfcn at 96
        feval(varargin{:});

Error in ==> test_gui_1 at 42
    gui_mainfcn(gui_State, varargin{:});

Error in ==>
@(hObject,eventdata)test_gui_1('pb1_wind_Callback',hObject,eventdata,guidata(hObject))


??? Error while evaluating uicontrol Callback

你有解决方法吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

我认为这link对您有益......