In a GUI, how can i create a matrix of several different inputs?
Show older comments
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
Azzi Abdelmalek
on 11 Feb 2013
To add an image, only jpg format is allowed.
Image Analyst
on 11 Feb 2013
Edited: Image Analyst
on 11 Feb 2013
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.
Answers (2)
Image Analyst
on 11 Feb 2013
0 votes
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
Gabi
on 11 Feb 2013
Image Analyst
on 11 Feb 2013
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.
Gabi
on 11 Feb 2013
Edited: Image Analyst
on 11 Feb 2013
Image Analyst
on 11 Feb 2013
What is the code for the 3 OK buttons? Which is "pushbutton2"?
Azzi Abdelmalek
on 11 Feb 2013
0 votes
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!