How to enter input variable from dialog box into text file?
1 次查看(过去 30 天)
显示 更早的评论
if true
% code
prompt={['What is your name?']};
title = 'Name Machine';
answer = inputdlg(prompt, title);
name = answer{1};
fileID = fopen('NameMachineFive.txt', 'w');
fprintf(fileID,['His name is %s.', name]);
fclose(fileID);
Ignore the top 2 lines and that was the original code. It was displaying "His name is " in the text document without the name. Then, dpb helped me out to fix it. Thanks dpb!
2 个评论
采纳的回答
dpb
2014-10-27
编辑:dpb
2014-10-27
fprintf(fileID,['His name is %s.', name]);
should be
fprintf(fileID,'His name is %s.', name);
You've put the variable name in a character string with the format string and left no output variable to be printed.
ADDENDUM
Intended to note but forgot--in your script you used title as a variable for the dialog box heading. This aliases the TMW-supplied Matlab function title of the same name so if you want to plot something later and add a plot title you'll get an error.
Avoid using Matlab functions as variable names--admittedly, when still learning it's not always easy to know. That's why the color coding in the Matlab editor is useful amongst other things.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!