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

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..

 Accepted Answer

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 Comments

Thanks for the reply! Even if I do that, it is not accepting the variables which are all declared other than input.
This is the error I'm getting when I'm doing calculation with the remaining variables. I've declared those variables in the listbox callback just below the for loop. Am I declaring the variables in the wrong callback? If so, where else should i declare them?
I've tried declaring inside the for loop also. Then also it is not working.
You'll have to post the code that shows how you are setting the fields in the handles structure (like SD). You will need to do something similar to input with all of your other fields that are "added" to the handles structure. If you do something like
handles.SD = %whatever
then this must be followed by
guidata(hObject,handles);
so that GUI version of handles is updated and all subsequent calls to the callbacks will receive the updated version of handles.
You also have to be very careful how you write your code - if the code in one callback expects a field to be defined in handles (like right now) which is set/initialized from another callback, then the order in which the user uses the GUI becomes very important: he/she can't use the popup menu (for example) until the listbox has been "used" (else the SD field won't be set). That's why it would be a good idea to define all of these fields within the OpeningFcn of your GUI. Set each with default data (or empty matrices)
function yourGuiName_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% define the user-defined fields
handles.SD = [];
handles.input = [];
% etc.
guidata(hObject,handles);
Then, elsewhere in your code, like in raw_popupmenu1_Callback, you would do something like
cla;
if ~isempty(handles.SD)
plot(handles.SD);
end
Or if you don't want to do it that way, then you should do something like
if isfield(handles,'SD')
plot(handles.SD);
end
Actually sir, I've tried everything you mentioned. But couldn't get the expected result. I've tried declaring in opening function, but it's not working since the mat file is loaded only after using the list box. Since, my code is very long, couldn't paste it here. So attaching the code. If you have time, pls have a look at it.
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...
Thank you sir.. Sorry for the late reply.. Will try this and will let you know..

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!