How to enter input variable from dialog box into text file?

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
dpb 2014 年 10 月 27 日
編集済み: dpb 2014 年 10 月 27 日
Please don't edit away the question -- makes the whole thing useless for anybody else going forward. I suggest re-editing to put back the original as it was.
That you "Accepted" an answer indicates the issue was resolved.
Muaaman
Muaaman 2014 年 11 月 11 日
Agreed. My apologies.
I have edited the question again to show the original code (couldn't find out how to remove the if true % code part), the problem, and who helped me.

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

 採用された回答

dpb
dpb 2014 年 10 月 27 日
編集済み: dpb 2014 年 10 月 27 日

0 投票

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.

2 件のコメント

Muaaman
Muaaman 2014 年 10 月 27 日
Thank you very much. It worked!
Muaaman
Muaaman 2014 年 11 月 11 日
Thank you in addition for the addendum dpb.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

質問済み:

2014 年 10 月 27 日

コメント済み:

2014 年 11 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by