How do you access table data to be used with basic operators?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Stephen23
2023년 7월 10일
편집: 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일
"All variables of your tables and timetables must have data types that support calculations."
채택된 답변
Jesse Finnell
2023년 7월 10일
댓글 수: 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.
추가 답변 (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
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
Hope this resolves your error.
댓글 수: 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]}
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!