How to set out put files to save at user selected path?

1 回表示 (過去 30 日間)
adi kul
adi kul 2016 年 6 月 6 日
回答済み: Stephen23 2016 年 6 月 7 日
Hello All, This question is in continuation with following question: http://in.mathworks.com/matlabcentral/answers/287928-select-a-folder-to-save-output-files
Now from above answer I was able to set and select the output folder but I am not able to save the files in there.
Here is what I tried. The following code is below the push button:
input = get(hObject,'String');
%checks to see if input is empty.
if(isempty(input))
h=msgbox('Please select the output folder');
end
guidata(hObject, handles);
selected_dir = uigetdir();
set(handles.edit27, 'String', selected_dir)
Now in my another push button where the calculations happen, I want to save the output images in above selected path. So this is what I have done. The following is the code which I am having the calculation push button callback:
saveas(gcf,fullfile(selected_dir,1),'jpg');
close(gcf);
I am getting following error.
Undefined function or variable 'selected_dir'.
What I want is to save 1.jpg as output file in user selected folder. Also I want to display "Please select out put folder" message if user forgets to select it.

採用された回答

Weird Rando
Weird Rando 2016 年 6 月 6 日
編集済み: Weird Rando 2016 年 6 月 6 日
You have to set selected_dir as a global variable so that other section of the gui can access it.
  5 件のコメント
Stephen23
Stephen23 2016 年 6 月 7 日
編集済み: Stephen23 2016 年 6 月 7 日
@Weird Rando: and I'm just pointing out why using globals is universally considered a bad way to write code. I didn't invent this idea: even the official MATLAB blogs give the advice to avoid using globals!
If you ask for advice from someone, which information would you prefer to be given:
  1. a reliable and robust solution, or
  2. a solution that is avoided by all experts ?
I know which one I would want to learn.
In any case my comment written not just for you, but also for anyone who reads this page and who should be informed that globals are a buggy and slow solution. They deserve to know this: would you expect to find good advice on MATLAB Answers, or buggy advice? Which would you want to use?
Muhammad Usman Saleem
Muhammad Usman Saleem 2016 年 6 月 7 日
you are awesome @Stephon

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

その他の回答 (1 件)

Stephen23
Stephen23 2016 年 6 月 7 日
Here is the MATLAB documentation page which explains all of the recommended ways to pass data between callbacks, including lots of working examples:
Note that globals are not listed.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by