如何在图形用户界面中绘制Matlab中的多个点?

如何在图形用户界面中绘制Matlab中的多个点?,matlab,Matlab,我需要在Matlab中创建一个GUI来绘制点(用户可以根据需要绘制任意多个点),然后允许用户使用最小二乘法绘制一条线 我有两个主要问题 我的代码没有在工作区中创建变量,因此我无法对数据执行任何操作 目前,我的程序只更新了我给出的一点 这是我目前的代码: function varargout = florin(varargin) % FLORIN MATLAB code for florin.fig % FLORIN, by itself, creates a new FLORIN or

我需要在Matlab中创建一个GUI来绘制点(用户可以根据需要绘制任意多个点),然后允许用户使用最小二乘法绘制一条线

我有两个主要问题

  • 我的代码没有在工作区中创建变量,因此我无法对数据执行任何操作
  • 目前,我的程序只更新了我给出的一点
  • 这是我目前的代码:

    function varargout = florin(varargin)
    % FLORIN MATLAB code for florin.fig
    %      FLORIN, by itself, creates a new FLORIN or raises the existing
    %      singleton*.
    %
    %      H = FLORIN returns the handle to a new FLORIN or the handle to
    %      the existing singleton*.
    %
    %      FLORIN('CALLBACK',hObject,eventData,handles,...) calls the local
    %      function named CALLBACK in FLORIN.M with the given input arguments.
    %
    %      FLORIN('Property','Value',...) creates a new FLORIN or raises the
    %      existing singleton*.  Starting from the left, property value pairs are
    %      applied to the GUI before florin_OpeningFcn gets called.  An
    %      unrecognized property name or invalid value makes property application
    %      stop.  All inputs are passed to florin_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 florin
    
    % Last Modified by GUIDE v2.5 16-May-2017 02:02:26
    
    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @florin_OpeningFcn, ...
                       'gui_OutputFcn',  @florin_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 florin is made visible.
    function florin_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 florin (see VARARGIN)
    
    % Choose default command line output for florin
    handles.output = hObject;
    
    % Update handles structure
    guidata(hObject, handles);
    
    % UIWAIT makes florin wait for user response (see UIRESUME)
    % uiwait(handles.figure1);
    
    
    % --- Outputs from this function are returned to the command line.
    function varargout = florin_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;
    
    
    
    function x_Callback(hObject, eventdata, handles)
    % hObject    handle to x (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 x as text
    %        str2double(get(hObject,'String')) returns contents of x as a double
    valx=str2num(get(handles.x,'String'));
    setappdata(0,'x',valx);
    
    % --- Executes during object creation, after setting all properties.
    function x_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to x (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
    
    
    
    function y_Callback(hObject, eventdata, handles)
    % hObject    handle to y (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 y as text
    %        str2double(get(hObject,'String')) returns contents of y as a double
    valy=str2num(get(handles.y,'String'));
    setappdata(0,'y',valy);
    % --- Executes during object creation, after setting all properties.
    function y_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to y (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
    
    
    % --- Executes on button press in pushbutton1.
    function pushbutton1_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    m=getappdata(0,'x');
    n=getappdata(0,'y');
    plot(m,n,'r.','MarkerSize',20);
    
    % --- Executes on button press in pushbutton2.
    function pushbutton2_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton2 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    

    问候弗洛林。基于这个问题,我可以理解您希望创建一个GUI,这样用户可以输入一组点,然后绘制它们,还可以使用最小二乘法找到并绘制最佳拟合线。在你的进程中,你会遇到两个问题

    对于第一个问题。如果希望通过m文件向工作区发送/创建变量,并从工作区获取变量,可以使用
    assignin
    evalin
    功能

    assignin('base','x',100);
    y=1+evalin('base','x');
    
    上面的短代码通过m文件在工作区中创建变量
    x
    ,然后将其作为变量
    y
    ,该变量将变为
    y=101
    。你可以在这里和这里阅读更多

    对不起,我不明白你的第二个问题


    你可以先试试这个。谢谢。

    我强烈反对使用这两个函数中的任何一个,因为它们都会产生意外行为。关于@Suever什么样的意外行为?如果用户在基本工作区中已经有一个名为
    x
    的变量,会发生什么情况?此外,应不惜一切代价避免评估及其亲属。从GUI获取数据有很多更优雅的方法。特别是对于指南,您可以创建一个函数以将数据作为输出返回。它将更新现有变量
    x
    。我不太明白你的最后一行,你的意思是不通过在工作区中创建变量?第一个问题是关于在工作区中创建变量,然后使用它的具体问题。