Can user choose desired column data from excel to matlab?

3 views (last 30 days)
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

Image Analyst
Image Analyst on 29 Apr 2014
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
Viktor G.
Viktor G. on 29 Feb 2020
Edited: Viktor G. on 29 Feb 2020
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
Image Analyst
Image Analyst on 29 Feb 2020
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)

Community Treasure Hunt

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

Start Hunting!