Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Jesse Finnell
Jesse Finnell le 10 Juil 2023
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);
standard = 1;
Error using /
Arguments must be numeric, char, or
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 commentaires
Stephen23 le 10 Juil 2023
Modifié(e) : Stephen23 le 10 Juil 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 le 10 Juil 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."

Connectez-vous pour commenter.

Réponse acceptée

Jesse Finnell
Jesse Finnell le 10 Juil 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);
standard = 1;
  2 commentaires
Stephen23 le 10 Juil 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 le 10 Juil 2023
Great question. I guess I should rephrase and say the simplest solution I am capable of.

Connectez-vous pour commenter.

Plus de réponses (3)

ProblemSolver le 10 Juil 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);
%% 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));
% 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);
I hope this helps!

Jayant le 10 Juil 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);
standard(row) = 1;
Follow this documentation for reference.
Hope this resolves your error.
  1 commentaire
Stephen23 le 10 Juil 2023
Or simply avoid duplicating all of the data by using the correct kind of brackets in the first place.

Connectez-vous pour commenter.

Peter Perkins
Peter Perkins le 17 Juil 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]}


En savoir plus sur Data Type Conversion dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by