[Matlab] How to use inputdlg string variable in text file?

In summary, the code prompts the user for their name using the inputdlg function and saves it to a text file. However, there was an issue with the fprintf function which has been resolved by removing the square brackets.
  • #1
muaaman
5
0
I am trying to display the input that I enter into the dialog box from "inputdlg" function into a text file.

This is what I've so far:

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);

The input dialog shows up and asks 'What is your name'? I enter it in, and then it closes. I go open the text file 'NameMachineFive.txt' and I get:

Code:
His name is

But when I enter into Matlab:

Code:
name

I get the name that I entered into the 'inputdlg' dialog box.

So my question is:

]How would I fix the code to get the name input entered into the text file?

I'd highly appreciate the assistance.
 
Physics news on Phys.org
  • #2
muaaman said:
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);
Remove the square brackets in the fprintf:
Matlab:
fprintf(fileID,'His name is %s.', name);
 
  • Like
Likes muaaman
  • #3
Thank you very much DrClaude.
 

1. How do I read a string variable from an inputdlg in Matlab?

To read a string variable from an inputdlg in Matlab, you can use the inputdlg function with the 'string' input argument. This will allow you to enter a string value in the input dialog and store it in a variable.

2. Can I use the inputdlg string variable to write to a text file in Matlab?

Yes, you can use the inputdlg string variable to write to a text file in Matlab. You can store the string variable in a variable and then use the fwrite or fputs functions to write it to a text file.

3. How can I save multiple string variables from an inputdlg in a single text file?

To save multiple string variables from an inputdlg in a single text file, you can use the fprintf function in a loop. This will allow you to write each string variable to a separate line in the text file.

4. How do I use the inputdlg string variable as a file name for saving data in Matlab?

You can use the inputdlg string variable as a file name for saving data in Matlab by concatenating it with the file extension and using it as the file name in the fopen function. For example, if your string variable is filename, you can use fopen([filename, '.txt'], 'w') to create a text file with the inputted file name.

5. Is it possible to use the inputdlg string variable as a variable name in Matlab?

Yes, it is possible to use the inputdlg string variable as a variable name in Matlab. You can use the eval function to evaluate a string as a variable name. For example, if your string variable is variable_name, you can use eval([variable_name, '= 10']) to create a variable with the inputted name and assign it a value of 10.

Similar threads

  • Programming and Computer Science
Replies
4
Views
750
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
391
  • Programming and Computer Science
2
Replies
65
Views
2K
Back
Top