1

I have a question regarding for loops. I have 10 textboxes that are named, text1_data, text2_data... text10_data.

What i want to do is to loop the thing so i don't have to write every single possible outcome by hand. (a should be able to be pretty big.)

i tried writing h=text{i}_data, but that did not work. Any ideas of what h should be equal to?

data=xlsread('nipmaterial.xlsx')
      a=(get(materialpopup_data,'value'))-1

      for i=1:10
          h=text{i}_data;
          set(h,'string',data(a,i))
      end
  • Put all your text boxes into a cell array instead of individual variables, then you can iterate them easily. – Daniel Jul 23 '14 at 08:30
  • Well, if you ask "how to put handles in a cell array", maybe the finer points of MATLAB are not for you yet. I'll give you the ugliest but shortest solution, then maybe you'll reflect upon what @Daniel said and try give it a go sometime, just for the sake of the right thing. So, the ugly solution `eval(sprintf('h=text%d_data;', i));` (MATLAB God; please forgive me for using `eval`! he made me....) –  Jul 23 '14 at 09:06
  • 1
    Every time someone calls eval() a baby seal dies! Sometimes they are the best solution but most often... not. – EJG89 Jul 23 '14 at 09:09
  • @EJG89 Hopefully what I wrote is dead code, that never gets to run. :-) –  Jul 23 '14 at 09:15
  • @EJG89 There is nothing wrong with `eval`. That code will compile like any other valid piece of code. Normally `eval` are to be avoided, but there are of course reason when it can be used. However, the way that I would recommend is using a struct array here, since `text` seems to be connected to multiple properties. – patrik Jul 23 '14 at 09:20
  • @patrik Well eval should be avoided at great cost for newbies in my opinion. Using eval the debugger is not able to run properly reliably since eval statements are (as you will know) compiled during run-time. – EJG89 Jul 23 '14 at 09:22
  • @CTS-Link That comment was really downgrading and unnecessary. I thought this page was for people who needs help (newbies), so I really don't see your point in saying I should not use the "finer parts" of matlab. I have to use a program to do what task I was given. if that is java matlab or anything else, i still would need to ask for help, since I haven't studied any programming at all. Also, i know what a cell array is and I know how to use it, my problem is that I mix it up with other things. Just thought you should know. – user3796906 Jul 24 '14 at 07:03

1 Answers1

2

For the purpose of demonstration first create a figure where to put the texts in:

clear all
close all
clc

% Create a figure for demonstration
figure(1)
xlim([0 2])
ylim([0 11])
hold on

Then when creating textboxes just assign the initial text box handles to a cell array:

% Create initial text boxes and store handle in the cell text_box.
text_box=cell(10,1);
for ii=1:10
    text_box{ii}=text(1,ii,'Initial text');
end

Than you can later call them again to change them to your liking:

% Call text boxes again to change the text:
for ii=1:10
    h = text_box{ii} % Retrieve handle from cell
    set(h,'string','Other text than initial text')
end
EJG89
  • 1,189
  • 7
  • 17