How do I store an input value to a variable each time?

kijuyt6re

1 commentaire

Stephen23
Stephen23 le 18 Oct 2022
Modifié(e) : Stephen23 le 18 Oct 2022
Original question retrieved from Google Cache:
How do I store an input value to a variable each time?
I'm trying to create a program that lets the user run the code and input a number for matlab to solve an equation to and store it in a variable. I only know how to do this manually like
a1 = input('Enter value of a1')
a2 = input('Enter value of a2')
I'm looking for a way to do this for one line of code for a which stores each succeeding input to their own variables. Can anyone help me with this? Thank you

Connectez-vous pour commenter.

 Réponse acceptée

See this snippet of code. Adapt as needed
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter values';
userPrompt = {'Enter a1 : ', 'Enter a2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
a = [caUserInput{1}, caUserInput{2}]

5 commentaires

Thanks! This really helped with making entering values cleaner! I just need to clarify, this code puts both values for caUserInput to variable a? What I meant to ask was if I could store the values separately to their own variables a1 to a5, with only one code for a. Is this possible? Thanks again!
I have to use a{1} to a{5} for another equation that looks like this
b{1} = constant+a{1}
b{2} = constant+a{2}
Doing this separately is making the code too long and I also have to graph the outcome which I think will make it difficult to do if they were separate. Thanks a lot for your help!
Stephen23
Stephen23 le 25 Juin 2022
Modifié(e) : Stephen23 le 25 Juin 2022
" What I meant to ask was if I could store the values separately to their own variables a1 to a5, with only one code for a. Is this possible?"
It is certainly possible, if you really want to force yourself into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug:
The MATLAB documentation specifically recommends against what you are trying to do: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
In contrast, indexing is neat, simple, and very efficient. Indexing is what you should use.
"I have to use a{1} to a{5} for another equation that looks like this...Doing this separately is making the code too long"
Do not add everything constant individually.
Do not repeat lines of code like that.
USE ARRAYS!!!
Adding the constant should take ONE operation:
A = [.. vector of values..]
B = A + constant
See how simple MATLAB code shoud be? That should be a big hint that your approach is complex, difficult, inefficient, buggy, inadvisable, and best avoided.
Remember that computers are stupid things that are only good at one thing: doing very simple tasks in loops. So when you copy-and-paste lines and lines of code like that, you are just doing the computer's job.
That's what I'm looking for! Thank you!!
Image Analyst
Image Analyst le 25 Juin 2022
Modifié(e) : Image Analyst le 25 Juin 2022
Yes, Stephen was right (thanks Stephen). You should not have used cell arrays. The user's response, caUserInput, had to be a cell array because that's what inputdlg() returns, but notice I immediately put them into a normal double vector.
So all you had to do was to add some number to "a", not create a new cell array called "b". You should avoid cell arrays where at all possible. They just complicate things.
See the FAQ to learn when they are appropriate to use and also to learn when/where to use parentheses, braces, or brackets.
By the way it made me chuckle when you said that having 2 lines to assign b would make the program too long. My programs are typically 3000 to 5000 lines long, and personally I wouldn't worry about my program being too long if it went from 3000 lines to 3002 lines. Thanks for accepting the Answer! 🙂

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by