Error: Undefined operator './' for input arguments of type 'table'

I have a +330,000,000 row .txt file (attached a 1000 row sample .txt) that I am attempting to perform a linear regression on using the following script:
Clm = ('sample.txt');
t = readtable(Clm, 'Delimiter','comma');
>> x = t(:,2);
>> y = t(:,4);
format long
b = x./y
yCalc1 = b*x;
scatter(x,y)
hold on
plot(x,yCalc1)
xlabel('Pressure')
ylabel('Depth')
title('Pressure vs Depth')
grid on
I get the following return error:
Undefined operator './' for input arguments of type 'table'.
I have tried
b = divide(x,y)
and
b = x/y
neither alternative works.
I also get the same error for the multiplication on:
yCalc1 = b*x;

 Akzeptierte Antwort

madhan ravi
madhan ravi am 29 Dez. 2018
If your using older version of matlab (prior to 2016b) use bsxfun to perform arithmetic operations.
doc bsxfun % read it

8 Kommentare

Hi madhan ravi! Thank you for your help, I am running R2018a.
oops ! it looks like your 2nd column is a datatime
I tried to incorporate bsxfun but recieved the following:
>> x = t(:,2);
y = t(:,4);
format long;
b = bsxfun(@rdivide,x,y);
yCalc1 = bsxfun(@times,b,x);
scatter(x,y)
hold on
plot(x,yCalc1)
xlabel('Pressure')
ylabel('Depth')
title('Pressure vs Depth')
Error using bsxfun
Operands must be numeric arrays.
Try this :
Clm = 'sample.txt'; % sample1.txt just check your correct file name
t = readtable(Clm)
y = t{:,4}; % use curly braces instead of paranthesis
x=t{:,2};
b = x./y
yCalc1 = b.*x; % . inbetween b and *
scatter(x,y)
hold on
plot(x,yCalc1) % my suggestion is to plot them in a separate figure because the scaling are different , remove hold on and replace it with figure
xlabel('Pressure')
ylabel('Depth')
title('Pressure vs Depth')
grid on
This worked! Thank you for helping me! You advice was very helpful and showed me exactly where I made errors.
Anytime :)
Mariko, your file has column headers, so the table you create with readtable has those as variable names. You will probably find your code easier to work with if you leverage those. For example
b = t.Pressure ./ t.Depth;
yCalc1 = b .* t.Pressure;
scatter(t.Pressure,t.Depth);
Braces are good for extracting multiple variables. Use a dot to extract one. Or at least do this
y = t{:,'Depth'};
x = t{:,'Pressure'};
Also forgot to mention: you may find that converting your table to a timetable opens some new doors.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by