Matlab user input variable name

  • Context: MATLAB 
  • Thread starter Thread starter anon0219
  • Start date Start date
  • Tags Tags
    Input Matlab Variable
Click For Summary
SUMMARY

This discussion focuses on how to allow user-defined variable names in MATLAB when performing operations on vectors. The user seeks to subtract two vectors and assign the result to a variable named by the user. The solution involves using the eval function, which can evaluate text strings as MATLAB commands, enabling dynamic variable creation based on user input.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of vector operations in MATLAB
  • Knowledge of the input function for user input
  • Basic understanding of the eval function in MATLAB
NEXT STEPS
  • Research the eval function in MATLAB for dynamic variable creation
  • Learn about best practices for using user input in MATLAB
  • Explore alternatives to eval for safer variable assignment
  • Investigate MATLAB's workspace management and variable naming conventions
USEFUL FOR

MATLAB programmers, data analysts, and anyone looking to enhance user interactivity in their MATLAB applications.

anon0219
Messages
1
Reaction score
0
Hi,

I am writing a program where I have the user input the name of two previously created vectors, the computer subtracts them and then generates a variable in the workspace. I was wondering if there was a way to let the user pick this name. I have so far (Y and N have been previously defined as 1 and 0):

subtract_data=input('Would you like to subtract any data sets? Y/N');
if subtract_data == 1
higher=input('Enter the name of the spectrum you would like to subtract from');
lower=input('Enter the name of the spectrum you are subtracting');
new_data=higher-lower;
filename=input('Input file name: ', 's');

...?

end

I am not sure where to go from here how to get the variable new_data to be titled what the user inputs as filename.

thanks
 
Physics news on Phys.org
anon0219 said:
Hi,

I am writing a program where I have the user input the name of two previously created vectors, the computer subtracts them and then generates a variable in the workspace. I was wondering if there was a way to let the user pick this name. I have so far (Y and N have been previously defined as 1 and 0):

subtract_data=input('Would you like to subtract any data sets? Y/N');
if subtract_data == 1
higher=input('Enter the name of the spectrum you would like to subtract from');
lower=input('Enter the name of the spectrum you are subtracting');
new_data=higher-lower;
filename=input('Input file name: ', 's');

...?

end

I am not sure where to go from here how to get the variable new_data to be titled what the user inputs as filename.

thanks

You can use the eval function to evaluate text strings:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/eval.html

>> bob = 'eye(3)'
>> eval(bob)

would run the command to create a 3x3 identity matrix. Really helpful (if inefficient) command since you can generate stuff on the fly, e.g. plotting a variable number of datasets on a 2D graph.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K