Is it possible to declare and function within another function? if not, Look at the code below

% --- Executes on button press in Browse1.
function Browse1_Callback(hObject, eventdata, handles)
ExPath1 = fullfile(FilePath, FileName)
set(handles.Filename1,'string',ExPath1)
%-----------------------------------------------------------------------------
(I am trying use the ExPath1 variable from my Browse function in my calculate function, IS IT POSSIBLE TO USE THE ExPath1 VARIABLE? because I need it in order to do the calculation.)
%-----------------------------------------------------------------------------
%Calculate button code
function Calculate1_Callback(~, ~, ~)
[x, error, stat1, stat2, stat3] = correctionModelworking1(ExPath1,'true',2,'true');

 采纳的回答

More specifically:
function Calculate1_Callback(~, ~, handles)
ExPath1 = get(handles.Filename1, 'string');
x, error, stat1, stat2, stat3] = correctionModelworking1(ExPath1,'true',2,'true');
Provided, that is, that your callback is being passed handles, as would be the case if you are using GUIDE.

4 个评论

(This is the error message that occured; what does it mean? and is there a way to fix it? thank you in advance, i really appreciate your help)
Undefined variable "handles" or class "handles.Filename1".
Error in GUI>Calculate1_Callback (line 360) ExPath1 = get(handles.Filename1, 'string');
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in GUI (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)GUI('Calculate1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
% --- Executes on button press in Calculate1.
function Calculate1_Callback(~, ~, ~)
% hObject handle to Calculate1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%[x, error, stat1, stat2, stat3] = correctionModelworking1('landsat5.xls', 'true', 2,'true');
global shift fileName;
[~, ~, ~, ~, stat3] = correctionModelworking1(fileName, shift,'false',handles); %#ok<NASGU>
clear shift
As I indicated in my code, you need
function Calculate1_Callback(~, ~, handles)
If you use my code as written then you will not need the "global"

请先登录,再进行评论。

更多回答(1 个)

You either have to pass it in as an input to the second function, or make the second function nested:
function foo(stuff)
a = pi;
function baz()
a = a+1;
end
end

类别

帮助中心File Exchange 中查找有关 Desktop 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by