0

I'm new in matlab, I've searched a lot but I didn't find my answer. I want to get data from user in a for loop and put that data in matrix. I used this code:

npattern=inputdlg('Enter the number of Patterns');
a=npattern(1,1);
for i=1 : a(1,1);
    r=inputdlg('Enter Data');
    end
end

But it doesn't work for me. What should I do now?

1
  • What doesn`t work exactly? What do you expect? Commented Nov 5, 2013 at 17:41

2 Answers 2

2

Assuming your r can contain strings (not just numbers):

npattern=inputdlg('Enter the number of Patterns');
a=str2num(npattern{1});
for ii=1:a;
    r{ii}=inputdlg('Enter Data');
end

Comments:

  • inputdlg returns a cell array of strings
  • it's bet not to use i as a variable (i is sqrt(-1) by default)
  • r in your code is overwritten at each iteration. Better use a cell array
  • there is one end too many
Sign up to request clarification or add additional context in comments.

Comments

1
x=inputdlg('Enter the number of Patterns');
data = str2num(x{:}); 
r = zeros(data, 1);
for i=1:data
    x = inputdlg('Enter Data');
    r(i, 1) = str2num(x{:});
end

2 Comments

Not sure it can be assumed that the second inputdlg should accept a single number (at each iteration).
Partial question, partial answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.