MATLAB: Xls import data from gui browse button selection

guiupdate

i have created a gui with a browse button to select a file. I then want this file to have the data imported. I created a static text box to show the file pathway of the selected folder and this works fine. I then created code (auto generated) for importing the data in a seperate m file and it worked perfectly specifying the file name. I then put this code into my main m file w the GUI and changed the filename to set(handles.text2_filename,'String',filename)but it doesn't automaticall carry out the calculations above that unless i put a breakpoint. How do i get the variables to appear in the workspace when the file is selected.
My code for importing data is as follows:
sheetName='Data';
[numbers, strings] = xlsread((set(handles.text2_filename,'String',filename)), sheetName);
if ~isempty(numbers)
newData1.data = numbers;
end
if ~isempty(strings) && ~isempty(numbers)
[strRows, strCols] = size(strings);
[numRows, ~] = size(numbers);
% Break the data up into a new structure with one field per row.
if strCols == 1 && strRows == numRows
newData1.rowheaders = strings(:,end);
end
end
% Create new variables with custom names
% in the base workspace from those fields.
for i = 1:size(newData1.rowheaders, 1)
assignin('base', genvarname(newData1.rowheaders{i}), newData1.data(i,:));
end

Best Answer

The results of a set command:
set(handles.text2_filename,'String',filename)
is not a filename, it's a structure. Just pass in filename directly instead of the output of a set() command. And don't use assignin(). There's no need for that. See the FAQ for ways to increase the scope (visibility) of your variables: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
Also, it's very important to note that if you have numbers and string headers, the cell numbers of the numbers and text cells are not in sync. Just look at the example in the help for xlsread(). For example if you have a row of headers and a row of numbers starting at A1, strings(1,1) will refer to A1, but numbers(1,1) will NOT refer to cell A1. It will refer to Excel cell A2. So you you think that numbers(9, 4) (row9, column4) will get you the number in Excel cell 'D9' (row9, column4), you're wrong. So be aware of that when you're indexing through the cell array.