In a GUI, how can i create a matrix of several different inputs?

The problem we are having is that we are trying to change a certain static text along with the changes of the variable n in the loop, whats happening is that when we input a number for the thickness its running through the whole loop without waiting for a user input in an editable text. It uses the the first user input throughout the entire loop. How can we make it so that this loop can take different values and store it in the thickness matrix?
CODE :
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
layers =str2num(get(handles.input1,'String'));
layers=str2num(get(handles.input1,'String'));
thickness = zeros(layers,1)
n=1
while(n <= layers)
n2=num2str(n)
set(handles.text44,'String',n2)
%thickness(n)=randi([1,3]);
thickness(n)=str2num(get(handles.input2,'String'))
continue
n=n+1
end

2 Comments

To add an image, only jpg format is allowed.
Well I saw it when it used to be embedded here. I didn't have to go to a different web site. Not sure why you didn't see it - maybe a browser difference or something.

Sign in to comment.

Answers (2)

This looks like it would be a very fast loop once you push pushbutton2. What makes you think that the loop is still running by the time you enter something into the edit box called "input2"?
Moreover, a much bigger problem is if you change input2 while the loop is running, and if it did take the updated value, that updated value would take effect at some unknown time. You have no idea what n is when the new value takes effect. Wouldn't that be a problem?

5 Comments

Thanks for your reply,
Well i dont want the loop to keep running when i enter a value for a input 2, basically i want to enter different values for input2 in the edit text so we can change every value inside of the thickness matrix. I know that is the problem but how can i fix it ?
Thanks again
layers=str2num(get(handles.input1,'String'));
input2Value = str2double(get(handles.input2,'String'));
thickness = input2Value * ones(layers,1)
n=1
while(n <= layers)
n2=num2str(n)
set(handles.text44,'String',n2)
drawnow; % Force update.
n=n+1
end
I took thickness out of the loop because it never changed in the loop if input2 doesn't change. I also got rid of "continue" because it prevented n from being incremented so you had an infinite loop. Then I added a drawnow so you'd see the results on the screen while it's doing its intensive loop.
Alright thanks for your help , i can see the loop working and its increasing by 1 as it should but its not taking an input for each value , please look at this picture for better explanation:
So i input the number for layers i want, then when i input the the required thickness for the first layer the loop is running but it doesnt wait for the next input for the second layer, it jumps directly to the end of the loop .
Thanks
What is the code for the 3 OK buttons? Which is "pushbutton2"?
The first OK button is push button 5:
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
n2='1';
set(handles.text44,'String',n2);
set(handles.text43,'String',n2);
While the second OK is pushbutton 2 :
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)layers =
str2num(get(handles.input1,'String'));
layers=str2num(get(handles.input1,'String'));
input2Value = str2double(get(handles.input2,'String'));
thickness = input2Value * ones(layers,1)
n=1
while(n <= layers)
n2=num2str(n)
set(handles.text44,'String',n2)
drawnow; % Force update.
n=n+1
end
Thanks for your help

Sign in to comment.

I'am not sur if I've understood, but I think you should remove a while loop, and use one ok pushbutton. You can increment the n and store it using userdata of one of your objects

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 11 Feb 2013

Community Treasure Hunt

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

Start Hunting!