How to display different columns from excel file using uitable on MATLAB GUI?

Good day! I would like to ask on how to display different columns from excel file using uitable on MATLAB GUI. I am planning to display columns B, D, and AV on the uitable (to be specific, the ranges are B2:B31, D2:31, and AV2:AV31). Uitable displays the data from excel using a push button.
I have tried the following codes but it displays the entire contents of the reference excel file. (I used Image Analyst's answer as reference from http://www.mathworks.com/matlabcentral/answers/119233-xlsread-for-many-distinct-columns-that-aren-t-side-by-side-in-the-ss)
function 1_Callback(hObject, eventdata, handles)
fileName = 'Database.xls';
[numbers,strings,raw] = xlsread(fileName);
colB = raw(2:31,2);
colD = raw(2:31,4);
colAV = raw(2:31,48);
set(handles.uitable1,'data',raw)
I also tried the following but I know it's obviously wrong.
function 1_Callback(hObject, eventdata, handles)
fileName = 'Database.xls';
[numbers,strings,raw] = xlsread(fileName, 'Total', 'B2:B31,D2:D31,AV2:AV31');%Total is the excel sheet name
set(handles.uitable1,'data',raw)
Can somebody suggest a way on how to assign different ranges of data from excel file to a singe uitable? Thank you.

Answers (1)

Mary - note the line of code that you are using to set the data within the uitable
set(handles.uitable1,'data',raw)
You are using raw rather than columns that you have extracted from raw. I suspect that you want something more like
set(handles.uitable1,'data',[colB colD colAV]);
Note how we concatenate the columns of interest using the square brackets.

2 Comments

Thank you for answering! Your suggestion works! But what if the data I put on the uitable must be saved on an excel file. How can this be done?
Uitable cols A, B, and C must be saved on excel file cols B, D, and AV.
Mary - presumably you have a push button that when pressed the data from the uitable will be saved to the Excel spreadsheet. So to get the data you would do something like
data = get(handles.uitable1,'data');
and then use http://www.mathworks.com/help/matlab/ref/xlswrite.html xlswrite> to write each column from data to the appropriate column in the spreadsheet. See the examples from the link.

Sign in to comment.

Products

Asked:

on 26 Sep 2015

Commented:

on 27 Sep 2015

Community Treasure Hunt

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

Start Hunting!