如何在 GUI 界面中在 Matlab 中绘制多个点

  • 本文关键字:绘制 Matlab GUI 界面 matlab
  • 更新时间 :
  • 英文 :


我需要在 Matlab 中创建一个 GUI 来绘制点(用户可以随心所欲地绘制点(,然后允许用户使用最小二乘方法绘制一条线。

我有两个主要问题。

  1. 我的代码不会在工作区中创建变量,因此我无法对数据执行任何操作。
  2. 目前,我的程序只是更新了我给出的一点。

这是我当前的代码:

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,以便用户可以输入一组点,然后绘制它们,并使用最小二乘方法查找和绘制最佳拟合线。在你的进步中,你会遇到2个问题。

对于第一个问题。如果要通过 m 文件向工作区发送/创建变量并从中获取变量,则可以使用 assigninevalin 函数。

assignin('base','x',100);
y=1+evalin('base','x');

上面的短代码通过 m 文件在工作区中创建了一个变量x,然后将其用于变量 y ,该变量将变为 y=101 。您可以在此处阅读更多内容 https://www.mathworks.com/help/matlab/ref/assignin.html 和 https://www.mathworks.com/help/matlab/ref/evalin.html。

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

您可以先尝试一下。谢谢。

最新更新