# How do you access table data to be used with basic operators?

2 ビュー (過去 30 日間)
Jesse Finnell 2023 年 7 月 10 日

I have attached some code to help bring context to my question. I have a table of imported data from Excel that I need to run some formulas on using if loops. One loop works fine as it only uses a single value from my data table, but I cannot get the second loop to work.
The second loop formula requires a value from the corresponding row in another column of the data table to be used in the formula, but I can't get it to run.
When using paretheses or curly braces I receive the error:
>> for row = 1:numel(STD);
if STD{row} == 'DWA'
standard(row) = 1-2.29*10^(-4)*(data(row,"Depth")/3.2808)^2+9.19*10^(-3)*(data(row,"Depth")/3.2808);
else
standard = 1;
end
end
Error using /
Arguments must be numeric, char, or
logical.
Is there a way to access these numerical values within the formula? I've tried dot indexing Value like in my previous code, but that receives an error as well. Do I need to use another nested loop? I'm still fairly inexperienced and have trouble diagnosing these problems.
##### 4 件のコメント2 件の古いコメントを表示2 件の古いコメントを非表示
Stephen23 2023 年 7 月 10 日

@Steven Lord: will that help with numeric data stored inside a cell array inside a table?:
Avoiding that data design would certainly help.
Steven Lord 2023 年 7 月 10 日
@Stephen23 It would not. The first condition given in the documentation for that functionality is:
"All variables of your tables and timetables must have data types that support calculations."

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

### 採用された回答

Jesse Finnell 2023 年 7 月 10 日
I found a solution to my problem that looks to be the simplest. I used the cell2mat function in conjunction with the table indexing. See the following code,
for row = 1:numel(STD);
if STD{row} == 'DWA'
standard(row) = 1-2.29e-4*(cell2mat(data{row,'Depth'})/3.2808)^2+9.19e-3*(cell2mat(data{row,'Depth'})/3.2808);
else
standard = 1;
end
end
##### 2 件のコメントなしを表示なしを非表示
Stephen23 2023 年 7 月 10 日
"I found a solution to my problem that looks to be the simplest. "
Why are you storing numeric data inside a cell array inside a table? The simplest solution is to fix that data design.
Jesse Finnell 2023 年 7 月 10 日
Great question. I guess I should rephrase and say the simplest solution I am capable of.

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

### その他の回答 (3 件)

ProblemSolver 2023 年 7 月 10 日
I wasn't sure if this what you were expecting;
to my understanding if you trying to access the "Depth" of the data table then you have use parantheses instead of curly braces. Additionalyy you forgot to call the value of the table using ".Value". Here, I have provided the optimized code version and perfomred some changes. Since I don't have the excel file and the structure of the excel file.
%% Test Sheet
%% Open data location
% find open Excel File (if multiple, chooses 'current')
DB = actxGetRunningServer('Excel.Application');
% choose correct tab
DBsheet = DB.ActiveWorkbook.Sheets;
DBsheet = DBsheet.get('Item', 15);
DBsheet.Activate;
%% Import data
% Specify range and import data
range = 'L5:O14';
data = DBsheet.Range(range).Value;
% Transpose the data
dataTranspose = data';
% Convert to table and assign variable names
data = array2table(dataTranspose, 'VariableNames', {'ATAD', 'TDS', 'Depth', 'Standard', 'SF', 'Design', 'Alt1', 'Alt2', 'Alt3', 'Alt4'});
%% Find modifiers
% Determine TDS factor
TDS = data.TDS;
ktds = ones(size(TDS)); % Preallocate ktds array
for row = 1:numel(TDS)
if TDS(row) > 1000
ktds(row) = exp(9.65e-5 * (2000 - 1000));
end
end
% Find standard factor
STD = data.Standard;
depth = data.Depth ./ 3.2808; % Divide the Depth values by 3.2808
standard = ones(size(STD)); % Preallocate standard array
for row = 1:numel(STD)
if strcmp(STD(row), 'DWA')
standard(row) = 1 - 2.29e-4 * depth(row)^2 + 9.19e-3 * depth(row);
end
end
I hope this helps!
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

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

Jayant 2023 年 7 月 10 日
You can use the table2array function to convert the data table to a numeric array and then access the values using parentheses () for indexing.
dataArray = table2array(data);
for row = 1:numel(STD)
if STD{row} == 'DWA'
depth = dataArray(row, strcmp(data.Properties.VariableNames, 'Depth'));
standard(row) = 1 - 2.29e-4 * (depth / 3.2808)^2 + 9.19e-3 * (depth / 3.2808);
else
standard(row) = 1;
end
end
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Stephen23 2023 年 7 月 10 日
Or simply avoid duplicating all of the data by using the correct kind of brackets in the first place.

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

Peter Perkins 2023 年 7 月 17 日
There are suggestions in this thread pointing in different directions. To answer the question as stated in the post's title, this example
demonstrates how to do calculations on data in tables. More recently, there is is
and this
which show how to use what Steve Lord alludes to in R2023a.
But if the data are in a cell array in a table, that's a horse of a different color. I can't tell what you actually have. Best I can guess is that this
dataTranspose = cell(columns, rows);
% Loop to transpose one row at a time
...
% Conver to table and assign variable names
data = array2table(dataTranspose,
is making a table all of whose variables are cell arry columns, with a scalar in each cell. That's a terrible way to store your data! I'm gonna guess that what you needed was cell2table. Compare:
cell2table({1 2; 3 4})
ans =
2×2 table
Var1 Var2
____ ____
1 2
3 4
array2table({1 2; 3 4})
ans =
2×2 table
Var1 Var2
_____ _____
{[1]} {[2]}
{[3]} {[4]}

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

### カテゴリ

Help Center および File ExchangeTables についてさらに検索

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by