How to access the data and passing it around when using GUI?

2 views (last 30 days)
Hello, I am new to matlab and trying to write a code for easier data filter for my colleague. By the way,i am using matlab R2011a.
i first wrote several functions to just import from excel file and did some data filter to exclude the unwanted infomations. and i had wrote a main function try to automate all the process.
function fgAnalysisMain (Filename,state)
RawData = importfile(Filename);
Data = fgDataFiltering(RawData,state);
end
The importfile function is matlab generated code. The fgDataFiltering function will take the imported data and filter the infomations through the conditioning (1,9) matrix called state.
when i run the main function in command window. the result Data will be a structure with maximum 9 field names, and each field will have 1 column of numerical data.
when i was working with command window, these functions work perfectly fine, but when i call this main function in the GUI code(shown below)
function btnAnalysis_Callback(hObject, eventdata, handles)
% hObject handle to btnAnalysis (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
states = zeros(1,9);
cbSOC_Callback(handles.cbSOC,eventdata,handles);
cbSOH_Callback(handles.cbSOH,eventdata,handles);
cbESR_Callback(handles.cbESR,eventdata,handles);
cbVoltage_Callback(handles.cbVoltage,eventdata,handles);
cbCurrent_Callback(handles.cbCurrent,eventdata,handles);
cbbms_Callback(handles.cbbms,eventdata,handles);
cbTemperature_Callback(handles.cbTemperature,eventdata,handles);
cbTimeMinutes_Callback(handles.cbTimeMinutes,eventdata,handles);
cbTimeSeconds_Callback(handles.cbTimeSeconds,eventdata,handles);
Filename = get(handles.file_address_output,'String');
fgAnalysisMain(Filename,states);
this Analysis button will check 9 checkbox status and update the state matrix introduced before and finally passing those parameters to the fgAnalysisMain function i wrote. but when i try the GUI, nothing showed up in workspace except the raw data from executing the importfile function.
I didn't setup any global variable in any functions and i tried to save output from fgAnalysisMain to variable named 'Data',and use assignin('base','Result',Data), the result Data is empty.

Answers (2)

Jan
Jan on 28 Feb 2014
Edited: Jan on 28 Feb 2014
This topic has been discussed frequently, but it is not easy to find the threads without knowing the matching keywords. Try:
  1 Comment
LinKun
LinKun on 28 Feb 2014
Edited: LinKun on 28 Feb 2014
Thanks for the response, I don't really know what i was looking for exactly, i'm just wondring why by calling the function in the GUI.m file is not the same as the command window. the fgAnalysisMain function outputs struct in command window and workspace for variables are created. but it became double when called in GUI.m 's button call back function. i tried to use evalin('base','Data = temp') function inside the fgDataFiltering() function but errors occured says i didn't define temp. but in fact i just defined 'temp' variable as a numerical array right before evalin.

Sign in to comment.


LinKun
LinKun on 28 Feb 2014
Nevermind, i fixed the code so that it doesn't have the fgAnalysisMain function, instead the two lines in the function were moved to the button call back function, and in this case it worked properly, i can store the struct result to handles.result and use assignin('base','result',handles.result) to create the workspace for the result. and the final code for the button looks like this:
states = zeros(1,9);
states = cbSOC_Callback(hObject,eventdata,handles,states);
states = cbSOH_Callback(hObject,eventdata,handles,states);
states =cbESR_Callback(hObject,eventdata,handles,states);
states =cbVoltage_Callback(hObject,eventdata,handles,states);
states =cbCurrent_Callback(hObject,eventdata,handles,states);
states =cbbms_Callback(hObject,eventdata,handles,states);
states =cbTemperature_Callback(hObject,eventdata,handles,states);
states =cbTimeMinutes_Callback(hObject,eventdata,handles,states);
states =cbTimeSeconds_Callback(hObject,eventdata,handles,states);
Filename = get(handles.file_address_output,'String');
handles.RawData = importfile(Filename);
handles.Data = fgDataFiltering(handles.RawData,states);
assignin('base','Result',handles.Data)
unfortunately for this one i have to pass states to every callback function for the check box and update it before passing it to the filtering function. but since it logically works for me, and doesn't consume too much computing speed, i will keep in this format because when i tried to save the states variable in handles.states, some random errors will be thrown in those checkbox callback functions
now I am wondering what happened, compare to my original code. where use a fgAnalysisMain function to call those two would cause the struct output to become double.

Community Treasure Hunt

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

Start Hunting!