How to returns to the current folder when i execut my gui?

4 次查看(过去 30 天)
Hello everybody, I have a problem with my gui. All the figs and functions are saved in a defined directory. If I execute the GUI and then i change the current folder since the matlab editor the GUI does not works. Are there any solution to return to the current folder where my gui is saved?
I try to use cd, but when i run the GUI and i change the current folder, where it is saved, it does not work.

回答(1 个)

Image Analyst
Image Analyst 2018-5-19
Simply keep track of the known folder, like in a field of handles, like handles.dataFolder. Then when you want to do anything with any files, put the base filename into full file and read it in:
fullFileName = fullfile(handles.dataFolder, 'whatever.dat');
You shouldn't need to know the folder where your program is. But if you really do, and you have Windows, see the attached function.
  2 个评论
Abel Romero
Abel Romero 2018-5-19
I think that i don't ask correctly the question. I will try to put an example:
All my figs and functions are saved in this path, for example: C:\Users\Abel\Documents\MATLAB\TFG_definitivo2\Project\Functions.
Then i execute the GUI, while it is executing i change the current folde in the comand window to: C:\Users\Abel\Documents\MATLAB\TFG_definitivo2\Project
Then i push a button in the GUI that is executing and it does not work and generates the following error: Undefined function or variable 'Welcome'.
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Welcome('load_project_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
So, the problem is that when i run the gui in the folder that i've saved it and i change the current folder it does not works.
Image Analyst
Image Analyst 2018-5-19
So it has nothing to do with the folder and everything to do with some function or variable called Welcome not being defined. It's evidently in a different folder than your main program and you do not have a path set to it. You can add a path either with the Set Path tool on the tool ribbon, or in your OpeningFcn of your GUI code with this code:
folder = 'C:\Users\Abel\Documents\MATLAB\TFG_definitivo2';
% Add all functions in that folder and subfolders of it to the search path so it will find the functions.
addpath(genpath(folder));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by