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

Jesse Finnell · Jul 11, 2023 · 1.9K views
Question
%% Test Sheet %% Open data location % find open Excel File (if multiple, chooses 'current')DB = actxGetRunningServer('Excel.Application'); % choose correct tabDBsheet = DB.ActiveWorkbook.Sheets;DBsheet = DBsheet.get('Item',15);DBsheet.Activate; %% Import data % Specify range and import datarange = 'L5:O14';data = DBsheet.Range(range).Value; % Setup transposition of data to fit table[rows, columns] = size(data);dataTranspose = cell(columns, rows); % Loop to transpose one row at a timefor row = 1:rows;    % Loop columns    for col = 1:columns;        % Transpose        dataTranspose{col, row} = data{row, col};    endend % Conver to table and assign variable namesdata = array2table(dataTranspose, 'VariableNames',{'ATAD','TDS','Depth','Standard','SF','Design',...    'Alt 1','Alt 2','Alt 3','Alt 4'}); %% find modifiers % Determine TDS factor - This works fineTDS = data{:,"TDS"};for row = 1:numel(TDS);    if TDS{row} > 1000        ktds(row) = exp(9.65*10^(-5)*(2000-1000));    else        ktds = 1;    endend% Find standard factor - this breaks as I need to access a Depth value that% corresponds to the row of the loopSTD = data{:,"Standard"};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;    endend   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.  
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Nov 20, 2025

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

 

Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!