Help with GUI - Push button

3 ビュー (過去 30 日間)
aurc89
aurc89 2014 年 8 月 21 日
コメント済み: aurc89 2014 年 8 月 21 日
Hello! I created a GUI with Matlab, that gives me two numbers as output. These two numbers are visualized into two 'Edit text' commands. Now, what I need to do is to save this two numbers in a .txt file, by using a 'Push button' command. Let's suppose that the tags associated to the 'Edit text' commands (where the two numbers are visualized) are num1 and num2, while the tag associated to the 'Push button' command is save . What I want to do when pressing the Push button save is: (1) choose the folder where to save the two numbers; (2) save the two numbers as a unique .txt file containing the two numbers (e.g. numbers_file.txt or number_file.dat). How can I translate this into a matlab code (i.e. the callback function of the push button save ?). Thank you

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 8 月 21 日
aurc89 - assuming that you have used GUIDE, the callback function for the save button would look something like
function save_Callback(hObject, eventdata, handles)
% grab the numbers from the two text widgets
val1 = char(get(handles.num1,'String'));
val2 = char(get(handles.num2,'String'));
% create the unique file name given these two numbers
filename = [val1 '_' val2 '_file.txt'];
% get the directory to save the data to
foldername = uigetdir;
% ensure the folder name is valid
if ischar(foldername)
% update the file name to include the folder name
filename = fullfile(foldername,filename);
% open the file
fid = fopen(filename,'w');
if fid>0
% write the data to file
fprintf(fid,'%s %s\n',val1,val2);
fclose(fid);
end
end
The above assumes that integers are in the num1 and num2 fields, so you may need to adapt the code for floats. Try the above and see what happens!
  3 件のコメント
Geoff Hayes
Geoff Hayes 2014 年 8 月 21 日
If you already know the path folder, then just replace
foldername = uigetdir;
with
foldername = '\Users\geoff\whatever\'; % or whatever path you wish
aurc89
aurc89 2014 年 8 月 21 日
Thanks a lot! :)

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

その他の回答 (1 件)

Iain
Iain 2014 年 8 月 21 日
This should be what you need:
value1 = get(num1,'String')
folder = uigetdir();
dlmwrite - though you might go for a csv instead of .txt...
  1 件のコメント
aurc89
aurc89 2014 年 8 月 21 日
The first two rows are ok. Once I choose the folder I need to write the name of the file I want to save and save it , but I don't know how

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

カテゴリ

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