How to change the iteration value of for loop by the use of edit text box?

3 visualizaciones (últimos 30 días)
I want to get the value of iteration of 'for' loop from the user by means of edit text box in matlab gui. Is it possible? I'm unable to get output if I use handles.
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
get(handles.figure1,'SelectionType');
if strcmp(get(handles.figure1,'SelectionType'),'open')
index_selected = get(handles.listbox1,'Value');
file_list = get(handles.listbox1,'String');
filename = file_list{index_selected};
if handles.is_dir(handles.sorted_index(index_selected))
cd (filename)
load_listbox(pwd,handles)
else
[path,name,ext] = fileparts(filename);
switch ext
case '.mat'
load(filename)
handles.DE = DE;
set(handles.listbox2,'String',handles.DE)
otherwise
try
open(filename)
catch ex
errordlg(...
ex.getReport('basic'),'File Type Error','modal')
end
end
end
N = length(handles.DE);
for i = 1:handles.input
x = handles.DE((i-1)*2048+1:i*2048);
[d,TDFx(i,:)] = BHM1(x);
xd = diff(x);
[d,TDFxd(i,:)] = BHM1(xd);
xi(1)=x(1);
for j=2:length(x)
xi(j)=x(j-1)+x(j);
end
[d,TDFxi(i,:)] = BHM1(xi);
end
end
In the above code, handles.input is the numeric value which I want to get from user through edit text box.
function segments_Callback(hObject, eventdata, handles)
% hObject handle to segments (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.input = str2double(get(hObject,'String'));
guidata(hObject,'String')
This is the edit text box call back. I'm getting the error as Reference to non-existent field 'input'.
Is it possible to rectify the error? Someone please help me..

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 18 de Sept. de 2014
Shanmukha - check your usage of guidata in the segments_Callback. You should be saving the updated version of handles and not the string 'String'
handles.input = str2double(get(hObject,'String'));
guidata(hObject,handles);
Now handles.input will be available in any callback that fires after it is set here.
You should guard against the case where handles.input has yet to be defined (and so avoid the Reference to non-existent field 'input' error). Either assign it a zero value in the OpeningFcn of your GUI, or only do the for loop if the field input exists
function listbox1_Callback(hObject, eventdata, handles)
% do stuff
if isfield(handles,'input')
for i = 1:handles.input
% do other stuff
end
end
So we only do the for loop if input is a field within the handles structure.
  6 comentarios
Geoff Hayes
Geoff Hayes el 22 de Sept. de 2014
You didn't try putting in "guards" against using fields that haven't been declared i.e. in raw_popupmenu1_Callback you could do something like
case 'SD'
if isfield(handles,'SD')
cla;
plot(handles.SD);
% etc.
end
I can easily generate the errors Reference to non-existent field 'SD'. or Reference to non-existent field 'SD_ti'. because you don't check to see if these fields are defined before using them. And these fields are only defined if a valid file is selected from the list box BEFORE you attempt to select a feature from the popup menu. So if you select a feature before a file, you will get an error.
So you can put in guards (like above) before you access fields, or you create empty fields (in the OpeningFcn) but then you would still need checks to see if the field is empty or not, or you set some sort of flag to say that a file has been loaded successfully.
In BHCM_TDA_OpeningFcn do
handles.output = hObject;
handles.input = 0;
handles.fileLoaded = false;
% Update handles structure
guidata(hObject, handles);
set(handles.segments,'string','0');
% etc.
In the listbox1_Callback, once all fields of handles has been set do
handles.mc7_ti = mean(TDFxi(:,9)); handles.sc7_ti = std(TDFxi(:,9));
handles.mc8_ti = mean(TDFxi(:,10)); handles.sc8_ti = std(TDFxi(:,10));
% set the file loaded flag to be true
handles.fileLoaded = true;
guidata(hObject, handles);
Then in your three popup menu callbacks ti_popupmenu3_Callback, td_popupmenu3_Callback and raw_popupmenu3_Callback, wrap all the existing code in the following block
if handles.fileLoaded
% etc.
end
Try doing the above...
Shanmukha priya V
Shanmukha priya V el 15 de Oct. de 2014
Thank you sir.. Sorry for the late reply.. Will try this and will let you know..

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by