Asking user is they want to save matrix to text file.

조회 수: 1 (최근 30일)
Tanner Larkey
Tanner Larkey 2018년 4월 15일
댓글: Tanner Larkey 2018년 4월 15일
I have a program that generates a matrix based on user inputs called T and I'm trying to ask the user if they would like to save this matrix to a text file. If they do I want the file name to be specified by the user. If they don't want to save it, the program can just end. Here is what I have so far:
text = menu('Would you like to save data to a text file?','Yes','No');
if text == 1
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 4월 15일
That looks okay so far, except I would recommend using a different variable name so you do not interfere with using the text() function

댓글을 달려면 로그인하십시오.

답변 (1개)

dpb
dpb 2018년 4월 15일
편집: dpb 2018년 4월 15일
While the above will work, menu has been deprecated; perhaps use questdlg instead...
option=questdlg('Save to text file?', ...
'Save File',...
'Yes', 'No','Yes');
% Handle response
switch option
case 'Yes'
fn=uiputfile(... % call the savefile dialog if 'yes'
case 'No'
return % nothing left to do, maybe...
end
Altho I'm a more "get me to the point" kinda' guy and for my own use I'd just go ahead and call uiputfile straighaway; the user can always cancel. Of course, if is a less-experienced user, perhaps the extra step of coaching is better; that's purely personal choice...
  댓글 수: 10
Walter Roberson
Walter Roberson 2018년 4월 15일
So? questdlg() can be closed by clicking on the x ?
^C should interrupt anything. It should interrupt questdlg too. And indeed in my test if you focus on the command window, control-C, click to focus on the command window again, and control-C again, then
Operation terminated by user during uiwait (line 81)
In questdlg (line 428)
uiwait(QuestFig);
For the point about ^C to have any relevance, questdlg would have to prevent that too.
Tanner Larkey
Tanner Larkey 2018년 4월 15일
Thanks for the help! Works great! I went with the menu based one just because that wasn't new to me (I'm quite the amateur at writing scripts if you couldn't tell). I'm sure there are much more efficient ways to do it, but this is just what's most comfortable right now.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by