How to share variables between two nested functions?
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a parent function, Mass_Spec_Formatter_Beta, which generates a GUI. The purpose of this GUI is to allow the user to select an Excel file and process that file using formatting choices they selectede from the GUI. In the main GUI function, there are two nested functions, a small one that runs when the 'File Selection' button is pushed (LoadDataFile), and the main one which runs when the 'Analyze Data' button is pushed (ProcessData). The purpose of the smaller function is to open a file selection window for the user and change the name of the button to reflect the name of the file chosen. The purpose of the second larger function is to proces the data.
The problem is this: I need the smaller function to export the variable 'Location' (which is the filepath for the selected excel file) to thew larger nested function so that it can read the file and process it. However, I can't get the small nested function to send its variables back to the main function. I've tried preallocating 'Location', but this doesn't help. Declaring it global also doesn't help.
I need the variable 'Location' which is created in the LoadDataFile function to be taken into the ProcessData function. Thanks!
% Set up the button to select the data file
FileButton = uibutton(rightgrid,'push','Text','No File Selected! Please Select a File.','Interruptible','off','ButtonPushedFcn',@(FileButton,event) LoadDataFile);
function Location = LoadDataFile
% Prompt the user to select the file
[file,filepath] = uigetfile({'*.xlsx';'*.csv'},'File Selector','Please Select a Mass Spec Data File to Open');
% If the file was not selected, end the program immediately
if file == 0
close all
return
end
Location = append(filepath,file); % Create a solid path to the data file
FileButton.Text = append('Selected File: ',file);
SettingButton.Enable = true;
SettingButton.Text = 'Analyze Data with Current Parameters';
end
% Set up the button to run the code
SettingButton = uibutton(rightgrid,'push','Text','Please Select a File to Continue','Interruptible','off','Enable','off','ButtonPushedFcn',@(SettingButton,event) ProcessData(box1,box2,box3,box4,box5,box6,FilterList,UIFig,SettingButton,Location));
% In the event the user decides to exit from the program, prompt
% them first
UIFig.CloseRequestFcn = @(src,event)my_closereq(src);
function my_closereq(UIFig)
selection = uiconfirm(UIFig,'Close the Processor?',...
'Confirmation');
switch selection
case 'OK'
delete(UIFig)
case 'Cancel'
return
end
end
%% Run the processing function only once the button confirming processing specifications have been pushed
function ProcessData(box1,box2,box3,box4,box5,box6,FilterList,UIFig,SettingButton,Location)
0 Commenti
Risposta accettata
David Hill
il 22 Feb 2022
If the functions are nested, then variables are carried over into all the nested functions and the variable names should turn green to indicate that they are available everywhere (similar to global variables). Make sure your all your end's are in the right place.
function GUI
Location=[];
function nested1
Location='myFile';
end
function nested2
Location='newFile';
end
end
1 Commento
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Workspace Variables and MAT Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!