PDA

View Full Version : [MATLAB] Ciclo for su handles oggetti gui


Qwertid
21-03-2009, 21:29
Ciao a tutti!
Ho creato una gui in matlab che ha vari oggetti tra cui 25 testi editabili che hanno un numero progressivo (es: handles.Rp1, handles.Rp2, ...., handles.Rp25).

Dovendo fare delle operazioni sulla gui che coinvolgono tutti questi 25 testi editabili (nel campo string e nel campo value) volevo evitare di dover settare i singoli campi per 25 volte (per un totale quindi di 50 linee di codice praticamente uguali a meno del numero dell'handles).

Avevo pensato di fare un ciclo for del tipo:

for i=1:25
var=strcat('handles.Rp',i);
set (var,'String','valore che mi serve)
set (var,'Value',valore che mi serve)
guidata(h0beject, handles)
end

Con il sistema che ho scritto su var assume correttamente i valori handles.Rp1, handles.Rp2, ... etc .

Il problema è che così facendo non me l'accetta più nella funzione set dicendomi che var deve essere un handles, mentre con lo strcat è visto come una stringa: come posso fare a rendere la stringa l'handles dell'oggetto corrispondente?


C'è una soluzione vero? Non devo essere costretto a scrivere 50 orribili righe di codice pressochè identiche, vero??? :D

Grazie mille a tutti!

Qwertid
22-03-2009, 12:09
Per far capire meglio lo scempio che c'è nel mio codice ne metto un piccolo frammento che è più che esplicativo :d


set(handles.thr0,'String',20)
set(handles.thr0,'Value',20)
set(handles.thr1,'String',20)
set(handles.thr1,'Value',20)
set(handles.thr2,'String',20)
set(handles.thr2,'Value',20)
set(handles.thr3,'String',20)
set(handles.thr3,'Value',20)
set(handles.thr4,'String',20)
set(handles.thr4,'Value',20)
set(handles.thr5,'String',20)
set(handles.thr5,'Value',20)
set(handles.thr6,'String',20)
set(handles.thr6,'Value',20)
set(handles.thr7,'String',20)
set(handles.thr7,'Value',20)
set(handles.thr8,'String',20)
set(handles.thr8,'Value',20)
set(handles.thr9,'String',20)
set(handles.thr9,'Value',20)
set(handles.thr10,'String',20)
set(handles.thr10,'Value',20)


E mi sono fermato a thr10, mentre nel mio codice si arriva fino a thr21 :cry:


Quale sarà mai sta benedetta funzione?

Qwertid
22-03-2009, 16:09
Ho risolto il problema!

Scrivo anche qui la soluzione così che magari può essere utile in futuro a qualcuno:


for i=0:24

var=handles.(['thr' num2str(i)]);
set (var,'String',20);
set (var,'Value',20);

end



Ciao! :)