Import values from excel to use it as content

5 views (last 30 days)
I know that this may sound complicated, but I will give it a shot anyway. I using the import data feature to pull data from an excel sheet. In this excel sheet I have 3 sets of results with 7 data points each. (Test 1, 2 and 3) the result can be either A B or C. In matlab I have a value for A B and C for each section. What I need help doing is to use the content of the cell and imput it in the matlab code.
I have been converting the excel data into three column vectors with categorical variables. One vector is the T1 column, the other is T2 and then T3. So for example if my first value of T1 column vector is A, I would like to write in matlab: result_T1_1=T1(1) and if A is = 3, then result_T1_1 would be equal to 3.
What I am not sure is if I shouls import the excel content as text or as categorical variables or as tables... there are many options with which I am not really familiar with.
This is how my excel looks like.
I auto generate the section of the code that imports data from excel:
This is the code I have been using:
clear; clc;
%% Import data from spreadsheet
% Script for importing data from the following spreadsheet:
%
% Workbook: C:\Users\alfre\Desktop\Book1.2.xlsx
% Worksheet: Sheet1
%
% Auto-generated by MATLAB on 11-Mar-2022 13:51:57
%% Setup the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 3);
% Specify sheet and range
opts.Sheet = "Sheet1";
opts.DataRange = "C3:E9";
% Specify column names and types
opts.VariableNames = ["T1", "T2", "T3"];
opts.VariableTypes = ["categorical", "categorical", "categorical"];
% Specify variable properties
opts = setvaropts(opts, ["T1", "T2", "T3"], "EmptyFieldRule", "auto");
% Import the data
tbl = readtable("C:\Users\alfre\Desktop\Book1.2.xlsx", opts, "UseExcel", false);
%% Convert to output type
T1 = tbl.T1;
T2 = tbl.T2;
T3 = tbl.T3;
%% Clear temporary variables
clear opts tbl
%A C and C values for test 1
A=5;
B=10;
C=20;
result_T1_1=T1(1)
result_T1_2=T1(2)
result_T1_3=T1(3)
result_T1_4=T1(4)
result_T1_5=T1(5)
result_T1_6=T1(6)
result_T1_7=T1(7)
%A B and C values for test 2
A=8;
B=15;
C=25;
result_T2_1=T2(1)
result_T2_2=T2(2)
result_T2_3=T2(3)
result_T2_4=T2(4)
result_T2_5=T2(5)
result_T2_6=T2(6)
result_T2_7=T2(7)
%A B and C values for test 2
A=3;
B=12;
C=19;
result_T3_1=T3(1)
result_T3_2=T3(2)
result_T3_3=T3(3)
result_T3_4=T3(4)
result_T3_5=T3(5)
result_T3_6=T3(6)
__________________________________________________________
Now this is the output:
result_T1_1 =
categorical
A
result_T1_2 =
categorical
A
result_T1_3 =
categorical
B
result_T1_4 =
categorical
A
result_T1_5 =
categorical
C
result_T1_6 =
categorical
B
result_T1_7 =
categorical
C
result_T2_1 =
categorical
A
result_T2_2 =
categorical
A
result_T2_3 =
categorical
A
result_T2_4 =
categorical
B
result_T2_5 =
categorical
C
result_T2_6 =
categorical
C
result_T2_7 =
categorical
C
result_T3_1 =
categorical
A
result_T3_2 =
categorical
B
result_T3_3 =
categorical
A
result_T3_4 =
categorical
B
result_T3_5 =
categorical
C
result_T3_6 =
categorical
C
result_T3_7 =
categorical
C
>>
_____________________________________________________________________
Now the desired output I would want it to be :
result_T2_1= 5
result_T2_2= 5
result_T2_3= 10
result_T2_4= 5
result_T2_5= 20
result_T2_6= 10
result_T2_7= 20
and so on...

Accepted Answer

Peter Perkins
Peter Perkins on 14 Mar 2022
You can index into a vector of your numic values with a categorical:
>> opts = detectImportOptions("Book1.2.xlsx")
>> opts.VariableTypes = ["double" "categorical" "categorical" "categorical"];
>> t = readtable("Book1.2.xlsx",opts)
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
t =
7×4 table
Test_ T1 T2 T3
_____ __ __ __
1 A A A
2 A A B
3 B A A
4 A B B
5 C C C
6 B C C
7 C C C
>> values1 = [5; 10; 20];
>> values1(t.T1)
ans =
5
5
10
5
20
10
20

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!