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

2 Ansichten (letzte 30 Tage)
Tanner Larkey
Tanner Larkey am 15 Apr. 2018
Kommentiert: Tanner Larkey am 15 Apr. 2018
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 Kommentar
Walter Roberson
Walter Roberson am 15 Apr. 2018
That looks okay so far, except I would recommend using a different variable name so you do not interfere with using the text() function

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 15 Apr. 2018
Bearbeitet: dpb am 15 Apr. 2018
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 Kommentare
Walter Roberson
Walter Roberson am 15 Apr. 2018
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 am 15 Apr. 2018
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by