Extract range from table

Hello,
I imported an existing excel sheet and now want to make a new variable which holds only certain columns of that sheet that I need (in my case participant IDs, randomisation conditions, etc., i.e. row 1, row 49, etc.). I have specified the ranges as variables (see below) but where do I put the range into the code?
% set path to where the session 1 protocol spreadsheet is
% datapath, filename, and sheet are already defined in my code
% import data (spreadsheet with subject overview)
% tildes are used for placeholder arguments
[~,~,Raw]=xlsread(fullfile(datapath,filename),sheet);
% make a table from the Raw cells
% datafile = cell2table(Raw)
Raw = cell2table(Raw);
% we only need certain columns: A=subject ID, AW=randomisation,..., so define needed columns and general range
% e.g. variable = data(line_from:line_to,column)
Subject_ID = Raw(2:end,1);
TT_randomisation = Raw(2:end,49);
Snack_weights = Raw(2:end,59:76);
Height = Raw(2:end,28)
Weight = Raw(2:end,27)
range = [Subject_ID,Height,Weight,TT_randomisation,Snack_weights];
Thank you!

7 件のコメント

KSSV
KSSV 2021 年 1 月 12 日
Use readtable instead of xlsread.
Adam Danz
Adam Danz 2021 年 1 月 12 日
> I have specified the ranges as variables (see below) but where do I put the range into the code?
I don't understand this question. The ranges are 2:end, aren't they? The are already in the code.
Anna
Anna 2021 年 1 月 12 日
Ah sorry, I'll try to clarify: I defined the variables for the desired ranges but I do not understand where exactly to put them in the xlsread command. However I am not trying to transfer it from xlsread to readtable as KSSV suggested but I still have to piece it together. My questions seem quite basic but I am absolute beginner so every hint is appreciated.
Adam Danz
Adam Danz 2021 年 1 月 12 日
You're indexing the variable Raw which is produced by xlsread so I'm confused about applying the indices to xlsread.
Unless you're using an old version of Matlab, I agree with KSSV's suggestion to use readtable (released in r2013b).
Anna
Anna 2021 年 1 月 12 日
Yes, I'll do that, I am just reading up on the documentation for readtable, thanks.
Anna
Anna 2021 年 1 月 15 日
So, readtable seems to be the better option. I imported the spreadsheet, choosing one specific sheet:
T=readtable(fullfile(datapath,filename), 'Sheet', 'sheet_name')
I want to add a range with the 'Range' name-value pair, but the problem is that I only need specific columns (e.g. column 1, 5, 10-15) but I can only specify one range with this name-value pair. One site suggests to use SpreadsheetImportOptions instead but I don't understand either how to tell it to make a table with only the specific columns I need.
Anna
Anna 2021 年 1 月 15 日
Ok I got it

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import from MATLAB についてさらに検索

タグ

質問済み:

2021 年 1 月 12 日

コメント済み:

2021 年 1 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by