xls import data from gui browse button selection

1 view (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 24 Aug 2012
Edited: Image Analyst on 24 Aug 2012
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.
  2 Comments
Krystina
Krystina on 24 Aug 2012
@image analyst: hi there, thanks for the reply. I would type in the filename directly, only it changes everytime it is used, which is why i was hoping to find a way to copy the selected file selected from the browse button?
Image Analyst
Image Analyst on 24 Aug 2012
Well if anything, you'd use get(), not set(), but that's not even the preferred way like the FAQ shows, but it will work. If one button lets the user browse for the filename and you stick it in the static text label, then you can get it in another callback by doing
filename = set(handles.text2_filename,'String');
[numbers, strings, raw] = xlsread(filename);

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!