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

3 次查看(过去 30 天)
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..

采纳的回答

Geoff Hayes
Geoff Hayes 2014-9-18
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 个评论
Geoff Hayes
Geoff Hayes 2014-9-22
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...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by