我需要在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)
答案 0 :(得分:0)
关心弗罗林。基于这个问题,我可以理解你想要创建一个GUI,使用户可以输入一组点,然后绘制它们,并使用最小二乘法找到并绘制最佳拟合线。在你的进步中,你会遇到两个问题。
第一个问题。如果您想通过m文件发送/创建,并从工作区中获取变量,则可以使用import matplotlib.pyplot as plt
import numpy as np
columns = ('Last', 'High', 'Low', 'Chg.', 'Chg. %', 'Time', 'T?')
rows = ['Gold', 'Silver', 'Copper', 'Aluminum']
data_list = np.random.randint(10,90, size=(len(rows), len(columns)))
scatter_x = (1, 2, 3)
scatter_y = (1224.53, 1231.76, 1228.70)
fig = plt.figure(1)
fig.subplots_adjust(left=0.2,top=0.8, wspace=1)
#Table - Main table
ax = plt.subplot2grid((4,3), (0,0), colspan=2, rowspan=2)
ax.table(cellText=data_list,
rowLabels=rows,
colLabels=columns, loc="upper center")
ax.axis("off")
#Gold Scatter - Small scatter to the right
plt.subplot2grid((4,3), (0,2))
plt.scatter(scatter_x, scatter_y)
plt.ylabel('Gold Last')
fig.set_size_inches(w=6, h=5)
plt.show()
和{{1}功能。
assignin
上面的简短代码通过m文件在工作区中创建变量evalin
,然后将其用于变量assignin('base','x',100);
y=1+evalin('base','x');
,它将变为x
。您可以在https://www.mathworks.com/help/matlab/ref/assignin.html和https://www.mathworks.com/help/matlab/ref/evalin.html了解更多信息。
抱歉,我不明白你的第二个问题。
你可以先试试这个。谢谢。