GUIDE - Selecting textbox by iterations - get(handle​s.***,'Str​ing')

Hi all, I was wondering if anyone knew of a more elegant way to get the values of a list of inputs. I have 10 (possibly more) edit box inputs and I want to populate a vector with their values IF there is a value present. I can do it this way:
speedVector = [];
timeVector = [];
for box = 1:10
switch box
case 1
speedVector(1) = str2double(get(handles.speed1,'String'));
timeVector(1) = str2double(get(handles.time1,'String'));
case 2
speedVector(2) = str2double(get(handles.speed2,'String'));
timeVector(2) = str2double(get(handles.time2,'String'));
case 3
speedVector(3) = str2double(get(handles.speed3,'String'));
timeVector(3) = str2double(get(handles.time3,'String'));
case 4
speedVector(4) = str2double(get(handles.speed4,'String'));
timeVector(4) = str2double(get(handles.time4,'String'));
case 5
speedVector(5) = str2double(get(handles.speed5,'String'));
timeVector(5) = str2double(get(handles.time5,'String'));
case 6
speedVector(6) = str2double(get(handles.speed6,'String'));
timeVector(6) = str2double(get(handles.time6,'String'));
case 7
speedVector(7) = str2double(get(handles.speed7,'String'));
timeVector(7) = str2double(get(handles.time7,'String'));
case 8
speedVector(8) = str2double(get(handles.speed8,'String'));
timeVector(8) = str2double(get(handles.time8,'String'));
case 9
speedVector(9) = str2double(get(handles.speed9,'String'));
timeVector(9) = str2double(get(handles.time9,'String'));
case 10
speedVector(10) = str2double(get(handles.speed10,'String'));
timeVector(10) = str2double(get(handles.time10,'String'));
end
if isnan(speedVector(box)) || isnan(timeVector(box))
speedVector(box) = [];
timeVector(box) = [];
break
end
end
but that is clearly not a very clean way to do it. I'd like to use a loop to systematically go to each box and get a value.
Thanks! - Jon

5 Comments

Just out of curiosity, why are you using an edit box if you are getting different values, why don't you put it in a list and it would be a lot easier to handle? Or you can also use a drop-down list.
I could probably use a list, I didn't even think of that. . . A drop down list would not work though. The user will enter two columns of data - one for speeds and another for times to reach these speeds. The list solution would probably look a bit strange but it would be easier to handle.
Does this answer your question?
I don't know what Mahdi means by "list." I don't know what a list is. If he means a listbox, or a popup (what MATLAB calls a drop-down list), neither one lets the user type in something, plus you can't put a text label in front of it nicely like you can with 20 edit fields. See my answer below.
I meant listbox to select data values from. I wasn't aware that he wanted the user to input these values (but I guess that's what an editbox is for!). Out of curiosity, wouldn't a uitable work as well?
Like you said, with a listbox you can select prepopulated items from the list. The user cannot enter new items for speed and time, which is what Jonathan wanted to do, into a listbox.
You could enter data in a grid also - just depends on what kind of "look" you want your GUI to have.

Sign in to comment.

 Accepted Answer

Why have a loop and switch statement? I see no need for either one at all. Just get every one, which you're doing anyway, without any loop!

4 Comments

I was hoping for a method that I could expand to more inputs as necessary - hence why I was so focussed on loops, haha!
The goal was to have a functioning version of something like this:
sHandle = 1:10;
for i = 1:10
speedVector(i) = str2double(get(handles.sHandle,'String'));
end
Where sHandle would indicate each edit box... as I increase the inputs to something more like 100 then it becomes extremely tedious to have to type (or find/replace) for each individual edit box.
But yes, you're definitely correct that the way I've done it is far overcomplicated - I was just trying to focus on doing it as an iteration so that it could be expanded to more inputs.
You know how many edit boxes you placed on your GUI. If you add two more, just copy and paste two lines, and edit them for the new names. That's simple. I think in an attempt to have it in a for loop, you're making it more complicated, not less!
Fair enough, I was kind of thinking of having it user-expandable in the future i.e. "Add more fields" kind of functionality.
Just for clarification, would it be possible to do what I'm asking? I can make it work regardless, but I don't have a complete grasp on handles and this seems like there would be a fairly simple solution to indexing components of a GUI... perhaps using UserData (?)
Thanks for taking your time to help me understand this, I really appreciate it.
Yes, but you'd have to either have the max number and hide the ones you don't want/need, or you'd have to create them dynamically with uicontrol().

Sign in to comment.

More Answers (0)

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!