Can user choose desired column data from excel to matlab?

Hi all!!
I want by clicking BROWSE button (GUI) choose the file (I'm going to do this be uigetfile() ), open it and select the column (for example A2:A50) to be importing to matlab as a X vector.
How to do this?
Thanks in advance!

 Accepted Answer

Well I imagine the user needs to see the data before making the decision about which column to import. So they either do that in Excel in advance and then you ask them in MATLAB, or you haul the whole workbook into MATLAB and display in a uigrid control and then ask them. You can ask them via inputdlg(), like this snippet:
% Ask user for a number.
defaultValue = 45;
titleBar = 'Enter a number';
userPrompt = 'Enter the column number';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Round to nearest integer in case they entered a floating point number.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
If they enter column letters, you'll need to use this: http://blogs.mathworks.com/pick/2010/04/16/from-excel-column-labels-to-numbers/

7 Comments

Thanks for your reply. Sorry for disturbing once more, but it's not clear for me how the user can choose any column data from your example code...?
Did you run the code snippet? Let's say you loaded all the data you got from xlsread() into a grid and they are looking at it on the GUI. Then let's say that they decide somehow that they want to use column 3. Then, when you run the code above and it asks them for what column they want to use, they would enter 3. You now have that number and can extract it from the data you read in from the workbook using xlsread().
OK, now I understand. Thanks a lot!!!!!!!!!!!!!
But what if i wanted to choose a specific variable from a previosly imported excel file as a table? Is there a way that the user can be asked which variable he needs from the table?
I would prefer if the variable can be chosen with some input option and not with the xyz.variable solution
Thank you
If the spreadsheet has the data labeled with a string for either orw headers or column headers, then you can use strcmpi() or contains() to see where in the workbook that particular chunk of data lives, then extract it from the cell array. For example, something like
[numbers, strings, raw] = xlsread(filename);
columnHeaders = raw(1,:); % or whatever...
for col = 1 : length(columnHeaders)
if strcmpi(columnHeaders{col}, whatUserIsLookingFor)
break;
end
end
Not sure why this doesn't work. I have a table with a lot of variables as different columns like time, temperature etc. so what i am trying to do is for the be able to choose which variable they would like to see (which would be as a column vector) without me manualy creating each column as a vector. Is there any way to do this? thanks for the help
T = array2table(sortrows(randi(99,10,5),1), 'VariableNames',{'Time','Temperature','Humidity','BaroPres','DewPt'});
Choose = @(col) T(:,strcmp(T.Properties.VariableNames,col));
ColumnYouNeed = input('which column do you need? ' ,'s');
C = Choose(ColumnYouNeed);
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed)
hold on
answer=input('Would you like to also plot another column? ','s');
while answer=='yes'
ColumnYouNeed = input('which column do you need? ' ,'s');
C = Choose(ColumnYouNeed);
plot(T.Time, table2array(C))
grid
legend(ColumnYouNeed)
end
Viktor, try these two snippets:
% Example 1: one plot per measurement:
hFig1 = figure;
variableNames = {'Time', 'Temperature', 'Humidity', 'BaroPres', 'DewPt'}
T = array2table(sortrows(randi(99,10,5),1), 'VariableNames', variableNames)
choice = listdlg('ListString', variableNames, 'PromptString',{'Select something to plot.'})
numChoices = length(choice);
for k = 1 : numChoices
thisChoice = choice(k);
subplot(numChoices, 1, k);
plot(T.Time, T{:, thisChoice}, 'b-', 'LineWidth', 2)
grid on;
xlabel('Time', 'FontSize', 15);
ylabel(variableNames{thisChoice}, 'FontSize', 15);
end
hFig1.WindowState = 'maximized';
% Example 2: all measurements on same plot:
hFig2 = figure;
% variableNames = {'Time','Temperature','Humidity','BaroPres','DewPt'}
% T = array2table(sortrows(randi(99,10,5),1), 'VariableNames', variableNames)
% choice = listdlg('ListString', variableNames, 'PromptString',{'Select something to plot.'})
% numChoices = length(choice);
for k = 1 : numChoices
thisChoice = choice(k);
plot(T.Time, T{:, thisChoice}, '-', 'LineWidth', 2)
grid on;
hold on;
end
xlabel('Time', 'FontSize', 15);
ylabel(variableNames{thisChoice}, 'FontSize', 15);
legend(variableNames(choice));

Sign in to comment.

More Answers (0)

Asked:

on 29 Apr 2014

Commented:

on 29 Feb 2020

Community Treasure Hunt

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

Start Hunting!