Passing a structure from GUI to a function

3 Ansichten (letzte 30 Tage)
Chen Gafni
Chen Gafni am 29 Nov. 2014
Kommentiert: Image Analyst am 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,
% --- 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 = 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);

Antworten (1)

Image Analyst
Image Analyst am 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
This will list all the field names of the first argument.
  2 Kommentare
Image Analyst
Image Analyst am 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.

Melden Sie sich an, um zu kommentieren.


Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by