[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.
 

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

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.

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.

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.

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.

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

Replies
5
Views
7K
Replies
4
Views
977
Replies
5
Views
2K
Replies
12
Views
3K
Replies
4
Views
3K
Replies
4
Views
2K
Back
Top