Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 10 de Jul. de 2023
Editada: Stephen23 el 10 de Jul. de 2023
@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
Steven Lord el 10 de Jul. de 2023
@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."

Iniciar sesión para comentar.

Respuesta aceptada

Jesse Finnell
Jesse Finnell el 10 de Jul. de 2023
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 comentarios
Stephen23
Stephen23 el 10 de Jul. de 2023
"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
Jesse Finnell el 10 de Jul. de 2023
Great question. I guess I should rephrase and say the simplest solution I am capable of.

Iniciar sesión para comentar.

Más respuestas (3)

ProblemSolver
ProblemSolver el 10 de Jul. de 2023
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!

Jayant
Jayant el 10 de Jul. de 2023
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
Follow this documentation for reference.
Hope this resolves your error.
  1 comentario
Stephen23
Stephen23 el 10 de Jul. de 2023
Or simply avoid duplicating all of the data by using the correct kind of brackets in the first place.

Iniciar sesión para comentar.


Peter Perkins
Peter Perkins el 17 de Jul. de 2023
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]}

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by