필터 지우기
필터 지우기

Passing a structure from GUI to a function

조회 수: 1 (최근 30일)
Chen Gafni
Chen Gafni 2014년 11월 29일
댓글: Image Analyst 2014년 11월 30일
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);

답변 (1개)

Image Analyst
Image Analyst 2014년 11월 29일
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
Image Analyst
Image Analyst 2014년 11월 30일
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 2014년 11월 30일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by