Asking User a Question with Variables
    4 views (last 30 days)
  
       Show older comments
    
    Emma Sellers
 on 4 Nov 2019
  
    
    
    
    
    Answered: Priyanka Saxena
 on 18 Jul 2023
            Why does this not work? How do I ask the user questions involving changing variables? 
3 Comments
Accepted Answer
  JESUS DAVID ARIZA ROYETH
      
 on 4 Nov 2019
        solution: 
answer=inputdlg({'nodes'});
nodes=str2double(answer(1,1));
nodematrix=Inf(nodes);
A=0;
B=1;
while A<nodes
    B=B+1;
    A=A+1;
    answer=input(['Enter resistance between ',num2str(A),' and ',num2str(B)])
end
4 Comments
  JESUS DAVID ARIZA ROYETH
      
 on 4 Nov 2019
				something like this?
answer=inputdlg({'nodes'});
nodes=str2double(answer(1,1));
nodematrix=Inf(nodes);
A=0;
B=1;
answer=[];
while A<nodes
    B=B+1;
    A=A+1;
    answer(end+1)=str2double(inputdlg(['Enter resistance between ',num2str(A),' and ',num2str(B)]));
end
disp(answer)
More Answers (2)
  Steven Lord
    
      
 on 4 Nov 2019
        For this you could use num2str as JESUS DAVID ARIZA ROYETH posted, but I'd prefer either creating a string array to pass to the input function (if you're using a release of MATLAB that contains the string class) or using sprintf. Here's how you can use string (this requires you to use release R2017a or later to create a string using double quotes.)
A = randi([1 10]); % A scalar integer value between 1 and 10 inclusive
x = input("What positive number when squared gives " + A^2 + "? ");
if x == A
    disp("Correct!")
else
    disp("Incorrect, the correct answer is " + A + ".")
end
For sprintf:
k = 2;
s = sprintf('The value of %d*pi is approximately %f.', k, k*pi)
0 Comments
See Also
Categories
				Find more on Interactive Control and Callbacks in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



