i'm new in Matlab and i want to fit equations using popupmenu button on matlab gui, but i still can't work for it.
this is the code i've been working on. let's say i want to get file from an excel format file using this
% --- 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)
handles.filename=uigetfile('*.xlsx');
filename=handles.filename;
set(handles.edit1,'String', filename)
data = xlsread(filename)
axes(handles.axes1)
t1 = data(:,1)
t2 = data(:,2)
plot(handles.axes1,t1,t2)
grid on
and then i use an excel file to fit equations with popup menu
popvalue = get(handles.popupmenu1, 'value')
data = xlsread(filename)
t1 = data(:,1)
t2 = data(:,2)
if (popvalue == 2)
[xdata, ydata] = prepareCurveData(t1,t2)
ft = fittype( 'Ms*(1-A/x-B/x^2)+(D*x)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
[fitresult{2}, gof(2)] = fit( xData, yData, ft, opts );
plot( handles.axes1, fitresult{2}, t1, t2 );
elseif (popvalue == 3)
[xData, yData] = prepareCurveData(t1, t2 );
ft = fittype( 'Ms*(1-A/x-B/x^2)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
[fitresult{3}, gof(3)] = fit( xData, yData, ft, opts );
plot( handles.axes1, fitresult{3}, xData, yData );
elseif (popvalue == 4)
ft = fittype( 'Ms*(1-A/x-B/x^2)+(D*x)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
end
and this an error log i get
Unrecognized function or variable 'filename'.
Error in testskripsi>popupmenu1_Callback (line 85)
data = xlsread(filename)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in testskripsi (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)testskripsi('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

 採用された回答

VBBV
VBBV 2021 年 4 月 3 日
編集済み: VBBV 2021 年 4 月 3 日

0 投票

%if
file=uigetfile('*.xlsx')
set(handles.edit1,'String', file)
data = xlsread(file)
Try above

2 件のコメント

VBBV
VBBV 2021 年 4 月 3 日
Since youre trying to assign the filename to handles structre. The name of file need to be declared in opening function. That's why it throws error
VBBV
VBBV 2021 年 4 月 3 日
Moreover, filename is not a standard GUI component like edit box or check boxes etc. So it needs to be declared before it can be assigned to handles structure

サインインしてコメントする。

その他の回答 (0 件)

質問済み:

2021 年 4 月 3 日

コメント済み:

2021 年 4 月 3 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by