Passing a structure from GUI to a function

3 views (last 30 days)
Chen Gafni
Chen Gafni on 29 Nov 2014
Commented: Image Analyst on 30 Nov 2014
Hello, I created a GUI that generates a multi-field structure which is passed as an argument to a function (in a separated m file) selected by the user. It worked well for a while, but now every time I run the GUI, when the selected function is executed it fails to receive the intended structure. The structure looks fine before it is passed to the function, but when I check the function's varargin it says '[1x1 struct]', and obviously it doesn't preserve any of the fields of the original struct (when I try to access any such field I get an 'Attempt to reference field of non-structure array' error).
Bellow is the piece of code from the GUI (a callback function associated with a push button) that generates the structure (designParams) and calls the user-selected function (filename). The specific function I call has the header: function dataAnalysis (varargin)
Any idea what I'm doing wrong? Thanks,
Chen
% --- Executes on button press in runButton.
function runButton_Callback(hObject, eventdata, handles)
% hObject handle to runButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get parameter values
designParams.titleFontSize = str2double(get (handles.titleFontSizeValue,'String'));
designParams.axesFontSize = str2double(get (handles.axesFontSizeValue,'String'));
designParams.tickFontSize = str2double(get (handles.tickFontSizeValue,'String'));
designParams.labelFontSize = str2double(get (handles.labelFontSizeValue,'String'));
designParams.fontType = get (handles.fontTypeValue,'String');
designParams.figureUnits = get (handles.figureUnitsValue,'String');
designParams.globalFigDim = get (handles.globalFigDim,'Value');
designParams.figureHeight = str2double(get (handles.figureHeightValue,'String'));
designParams.figureWidth = str2double(get (handles.figureWidthValue,'String'));
designParams.globalYScale = get (handles.globalYScale,'Value');
designParams.keepTitles = get (handles.keepTitles,'Value');
designParams.addDataLabel = get (handles.addDataLabels,'Value');
designParams.addLegend = get (handles.addLegend,'Value');
designParams.askFolder = get (handles.askFolder,'Value');
for colorInd=1:5
RColor= strcat('str2double(get (handles.RColor',num2str(colorInd),',''String''))');
GColor= strcat('str2double(get (handles.GColor',num2str(colorInd),',''String''))');
BColor= strcat('str2double(get (handles.BColor',num2str(colorInd),',''String''))');
designParams.colors(colorInd,1)=eval(RColor);
designParams.colors(colorInd,2)=eval(GColor);
designParams.colors(colorInd,3)=eval(BColor);
end
designParams.colors = designParams.colors/255;
% Open function for data analysis
filename = uigetfile ('*.m','Select the MATLAB code file');
[pathstr,name,ext] = fileparts(filename);
filename = str2func(name);
filename (designParams);

Answers (1)

Image Analyst
Image Analyst on 29 Nov 2014
You forgot to show the code in the callback where you're actually calling dataAnalysis() and passing designParams to it.
If in dataAnalysis, it says that the input is a struct 1x1 that doesn't mean it has no fields. It will have fields. Just try this
a.f = 10;
whos a
See? So the fieldnames are probably not what you're using. Perhaps the capitalization is wrong. What does this say in dataAnalysis
filenames(varargin{1})
This will list all the field names of the first argument.
  2 Comments
Image Analyst
Image Analyst on 30 Nov 2014
Chen's "Answer" moved here since it's not an "Answer" to the original question but a reply to me:
Thank you for the answer. Actually the code I submitted did include calling the destination function - I ask the user to select an m file and use its name as a function name:
% Open function for data analysis
filename = uigetfile ('*.m','Select the MATLAB code file');
[pathstr,name,ext] = fileparts(filename);
filename = str2func(name);
filename (designParams);
This code does run the desired function, but the structure itself is not passed as I expected. If I try to pass other types of arguments (e.g. double) to the function they are passed correctly. I typed whos designParams in the callback function and got:
Name Size Bytes Class Attributes
designParams 1x1 2892 struct
But when I typed whos varargin in dataAnalysis I got:
Name Size Bytes Class Attributes
varargin 1x1 3004 cell
If it preserved the desired information I don't know how to access it. Any advice?
Image Analyst
Image Analyst on 30 Nov 2014
Well of course varargin is a cell - we know that. You need to extract the variable from the varargin cell array first. What does this say in dataAnalysis:
fprintf('Number of arguments = %d\n', nargin);
dp = varargin{1} % Extract designParams from the first cell
whos dp % Show infomation about it.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!