Returning an array to the command line (or another function) from a GUI

4 ビュー (過去 30 日間)
Douglas Anderson
Douglas Anderson 2013 年 12 月 9 日
コメント済み: Douglas Anderson 2013 年 12 月 10 日
Hi. I have looked at video and Answers to try to figure this out, and don't seem to have it right.
I have a GUI function that reads an Excel file (picked with uipickfiles , very nice), plots the raw data, and then uses dualcursor (also very nice) to select the appropriate data, and plot that in a separate axis. Good so far. Now, I need to get the windowed data out of the function!
The data is stored in "plot_me".
When I run the following code, I get an error of "Attempt to reference field of non-structure array." Sorry that it is kind of long. There are commented lines that show some of my attempts. Any suggestions are much appreciated. Thank you!
Doug
function [out_file,varargout] = scloopgui3(varargin)
% SCLOOPGUI3 MATLAB code for scloopgui3.fig
% SCLOOPGUI3, by itself, creates a new SCLOOPGUI3 or raises the existing
% singleton*.
%
% H = SCLOOPGUI3 returns the handle to a new SCLOOPGUI3 or the handle to
% the existing singleton*.
%
% SCLOOPGUI3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SCLOOPGUI3.M with the given input arguments.
%
% SCLOOPGUI3('Property','Value',...) creates a new SCLOOPGUI3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before scloopgui3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to scloopgui3_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help scloopgui3
% Last Modified by GUIDE v2.5 09-Dec-2013 14:54:58
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @scloopgui3_OpeningFcn, ...
'gui_OutputFcn', @scloopgui3_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before scloopgui3 is made visible.
function scloopgui3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to scloopgui3 (see VARARGIN)
handles.component_number = 2;
% Choose default command line output for scloopgui3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes scloopgui3 wait for user response (see UIRESUME)
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = scloopgui3_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
%handles.output = handles.plot_me;
varargout{1} = handles.output;
out_file = handles.plot_me;
%varargout{1} = get(handles.plot_me);
%final_wave = handles.plot_me;
delete(handles.figure1);
% --- Executes on button press in Plot_Data.
function Plot_Data_Callback(hObject, eventdata, handles)
% hObject handle to Plot_Data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%plot_me = get(handles.plot_me);
compo = handles.component_number;
all_data = handles.plot_me;
this_data = all_data(:,compo);
plot(handles.axes1,this_data);
% Old version: plot(handles.axes1,handles.plot_me);
% --- Executes on button press in Get_File_Pushbutton.
function Get_File_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to Get_File_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
file_name = uipickfiles('FilterSpec','*.xlsx','out','ch');
[aa,~,~] = xlsread(file_name);
handles.plot_me = aa; % commented out selection of 2 to start(:,2);
handles.left_cursor = 100;
handles.right_cursor = length(aa) - 100;
guidata(hObject,handles);
% --- Executes on button press in Close_Pushbutton.
function Close_Pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to Close_Pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get the current position of the GUI from the handles structure to pass to
% the modal dialog
pos_size = get(handles.figure1,'Position');
% Call modaldlg with the argument 'Position'
user_response = modaldlg('Title','Confirm Close');
switch user_response
case {'No'}
% Take no action
case {'Yes'}
% Prepare to close GUI application window
% .
% .
% .
delete(handles.figure1)
end
function Start_Window_Callback(hObject, eventdata, handles)
% hObject handle to Start_Window (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of Start_Window as text
% str2double(get(hObject,'String')) returns contents of Start_Window as a double
handles.Start_time = str2double(get(hObject,'String'));
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function Start_Window_CreateFcn(hObject, eventdata, handles)
% hObject handle to Start_Window (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function End_Window_Callback(hObject, eventdata, handles)
% hObject handle to End_Window (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of End_Window as text
% str2double(get(hObject,'String')) returns contents of End_Window as a double
handles.End_time = str2double(get(hObject,'String'));
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function End_Window_CreateFcn(hObject, eventdata, handles)
% hObject handle to End_Window (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Window_Plot_pushbutton.
function Window_Plot_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to Window_Plot_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
start_plot = handles.Start_time;
end_plot = handles.End_time;
compo = handles.component_number;
plot_me = handles.plot_me(:,compo);
plot_me = plot_me(start_plot:end_plot);
plot(handles.axes2,plot_me);
% --- Executes when selected object is changed in uipanel2.
function uipanel2_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uipanel2
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.
case 'transverse_radiobutton1'
handles.component_number = 2; % Code for when transverxe_radiobutton1 is selected.
case 'vertical_radiobutton2'
handles.component_number = 3; % Code for when radiobutton2 is selected.
case 'longitudinal_radiobutton3'
handles.component_number = 4; % Code for when togglebutton1 is selected.
end
guidata(hObject,handles);
% --- Executes on button press in add_cursors.
function add_cursors_Callback(hObject, eventdata, handles)
% hObject handle to add_cursors (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
left_cursor_start = handles.left_cursor;
right_cursor_start = handles.right_cursor;
dualcursor([left_cursor_start right_cursor_start],[],[],[],handles.axes1);
% --- Executes on button press in get_cursor_locations_and_plot.
function get_cursor_locations_and_plot_Callback(hObject, eventdata, handles)
% hObject handle to get_cursor_locations_and_plot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cursor_locs = dualcursor;
handles.Start_time = cursor_locs(1);
handles.End_time = cursor_locs(3);
start_plot = handles.Start_time;
end_plot = handles.End_time;
compo = handles.component_number;
plot_me = handles.plot_me(:,compo);
plot_me = plot_me(start_plot:end_plot);
plot(handles.axes2,plot_me);
%handles.output = plot_me;
guidata(hObject,handles);
% --- Executes on button press in save_data.
function save_data_Callback(hObject, eventdata, handles)
% hObject handle to save_data (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%handles.output = handles.plot_me;
guidata(hObject,handles);
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
if isequal(get(hOobject,'waitstatus','waiting'))
uiresume(hObject);
else
delete(hObject);
end
  2 件のコメント
Walter Roberson
Walter Roberson 2013 年 12 月 9 日
Which line are you getting the error message on?
Douglas Anderson
Douglas Anderson 2013 年 12 月 10 日
Walter
I have kept on trying and always get the message from this part of the code:
% --- Outputs from this function are returned to the command line.
function varargout = scloopgui3_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
%handles.output = handles.plot_me;
%varargout{1} = handles.output;
%handles.out_file = handles.plot_me;
out_file = handles.plot_me;
%varargout{1} = get(handles.plot_me);
%final_wave = handles.plot_me;
guidata(hObject,handles);
delete(handles.figure1);
I have changed and commented a few things I have tried, and it's always the same "Attempt to reference field of non-structure array".
By the way, you have helped before -- you are really good! Thanks.
Doug

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2013 年 12 月 10 日
Did I ever mention how much I hate GUIDE? ;-)
Hypothesis: for some reason handles is empty at that point.
Before opening the GUI,
edit guimainfcn.m
which is a file supplied as part of GUIDE.
Look down to roughly line 262. There should a something of the form
if nargout
[...] = feval(....)
else
feval(.....)
end
The feval() are the place(s) that the output function gets invoked. The local variable gui_Handles is passed in to the feval(). So we need to find out what gui_Handles is set to.
Go backwards in the file just a few lines. You should find something that looks like
gui_Handles = guidata(gui_hFigure);
else
gui_Handles = [];
end
so we can see that under some circumstances gui_Handles might be deliberately [].
My reading of the source above that would lead me to expect that it would normally be set to guidata. But let's put that to the test. Put in breakpoints at both assignment lines, and then invoke the gui you have created, and see where it stops.
If it stops at the deliberate assignment of [] then we would know how handles got to be [] in the callback, if not why. If it gets to that point, at the debug prompt see if
guidata(gui_hFigure)
returns anything useful. If it does, then the work-around would be: in your output function, at the beginning, add
handles = guidata(hObject);
If, though, the debugger stopped with guidata() being assigned to gui_Handles, then at that point ask the debugger
gui_hFigure
size(gui_hFigure)
ishghandle(gui_hFigure)
we would expect to see a non-zero gui_hFigure. We would expect to see gui_hFigure being 1 x 1. But if the ishghandle() fails then the implication would seem to be that the figure got deleted somewhere in your code, which would require more investigation.
  1 件のコメント
Douglas Anderson
Douglas Anderson 2013 年 12 月 10 日
Walter
Thank you for the insight. I've accepted the answer, though I still haven't gone through all of your recommendations. I've sufficiently buggered up the code trying to fix this problem that I think I need to go back and start afresh on parts of it, trying to keep your suggestions in mind.
I'll let you know (with another comment) sometime soon when it DOES work!
Thanks again.
Doug

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by