I have a script which searches for strings in files located in a folder.
I have a GUI made from appdesigner which has a button for where to pick your folder as well as where to pick your output file (to dump the gathered data to)
However, it is not letting me carry over variables from one function to another in the app designer's code view.
I have three buttons:
First one to open the folder you want to search in
% Button pushed function: FoldertoSearchButton
function FoldertoSearchButtonPushed(app, event)
fileLoc = uigetdir;
end
Second to choose the output file
% Button pushed function: FileButton
function FileButtonPushed(app, event)
outputFile = uigetfile('*.xls*');
end
and third to output the rejected file (ones which failed to have the given user-defined string in them):\
% Button pushed function: FileButton_2
function FileButton_2Pushed(app, event)
rejectFile = uigetfile('*.xls*');
end
end
I then have function located on the Start button callback which initializes the script.
% Button pushed function: StartButton
function StartButtonPushed(app, event)
stmFileSearch(app,fileLoc,outputFile,rejectFile)
end
The problem I'm having is it's giving me errors such as fileLoc is not defined, even when I have in the first button.

3 件のコメント

Nom
Nom 2019 年 9 月 12 日
編集済み: Nom 2019 年 9 月 12 日
This problem was solved by using global variables
so in this case defining
global fileLoc
fileLoc = uigetdir;
in the respective buttons and then calling the global variable in the StartButton
global fileLoc
global outputFile
global rejectFile
stmFileSearch(app,fileLoc,outputFile,rejectFile)
This is definitely not the preferred way of solving this problem, but it works.
If anyone else has any better alternatives, I would love to learn.
Stephen23
Stephen23 2019 年 9 月 13 日
編集済み: Stephen23 2019 年 9 月 13 日
"This is definitely not the preferred way of solving this problem"
Very true.
If anyone else has any better alternatives, I would love to learn.
Tthe MATLAB documentation is the best place to learn how MATLAB works:
Nom
Nom 2019 年 9 月 18 日
Great link/resource, Thank you!

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

 採用された回答

Adam Danz
Adam Danz 2019 年 9 月 13 日
編集済み: Adam Danz 2019 年 9 月 13 日

5 投票

Avoid using global variables.
Instead, you can define a new property of your app that stores the fileLoc data. The process is explained here but I'll summarize what you need to do below.
  1. From the editor tab in App Designer, select the red "Property" dropdown button at the top and select "Private Property". This will add a property definition to a properties block.
  2. Within that newly added section, you can define any variable name that will store your fileLoc data (see section 1 below).
  3. In your FoldertoSearchButtonPushed() function, save the fileLoc to the app (see section 2 below).
  4. Now you can access the fileLoc property anywhere within your gui (see section 3 below).
% Section 1
properties (Access = private)
fileLoc = ''; % Directory chosen by user in FoldertoSearchButtonPushed()
end
% Section 2
function FoldertoSearchButtonPushed(app, event)
fileLoc = uigetdir;
app.fileLoc = fileLoc;
end
% Section 3
function StartButtonPushed(app, event)
stmFileSearch(app,app.fileLoc,outputFile,rejectFile)
% ^^^^^^^^^^^
end

7 件のコメント

Nom
Nom 2019 年 9 月 17 日
Thank you Adam,
I will implement this and give it a go as soon as I can, I love learning the proper way of doing this!
Thank you!
Adam Danz
Adam Danz 2019 年 9 月 17 日
Glad I could help!
Nom
Nom 2019 年 9 月 18 日
Perfect!
Just implemented it and it works flawlessly.
One small change I had to make (due to an error)
was just in section 2:
% Section 2
function FoldertoSearchButtonPushed(app, event)
app.fileLoc = uigetdir;
end
Just had to remove the two lines and replace it with
app.fileLoc = uigetdir;
It acomplishes the same thing, I love it!
Adam Danz
Adam Danz 2019 年 9 月 18 日
Thanks for the feedback! Currious, what was the error, if you recall?
Nom
Nom 2019 年 9 月 18 日
編集済み: Nom 2019 年 9 月 18 日
My apologises, it wasn't an error par se , however it kept recommending me to change it to
app.fileLoc = uigetdir;
app.fileLoc = app.fileLoc;
At which I thought to just changed it to
app.fileLoc = uigetdir;
This change made no differece for the rest of my code when I called back to it using
app.fileLoc
Prabhakar Vallury
Prabhakar Vallury 2023 年 3 月 16 日
I'm having similar problems with global variables and tried the above. One big problem I face is that I need to save these property variables into a .mat file. Using the above example,
save ('file1.mat', 'app.fileLoc') comes back with an error saying it's not a valid variable. Almost there, would appreciate any help from this group. TMIA.
Joe
Steven Lord
Steven Lord 2023 年 3 月 16 日
The save function requires variable names not expressions when indicating what subset of data in the workspace to save. 'app.fileLoc' is an expression. Store the value of that property to a local variable in the function where you want to call save then save that local variable.
theFile = app.fileLoc;
save('file1.mat', 'theFile')

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Develop Apps Using App Designer についてさらに検索

製品

リリース

R2017b

質問済み:

Nom
2019 年 9 月 12 日

コメント済み:

2023 年 3 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by