How do I input a table column of values into a formula?
조회 수: 22 (최근 30일)
이전 댓글 표시
Hello, I am new to MatLab. Here is some simple code I would greatly appreciate some help on.
I am given an excel spreadsheet of dates and a value of length (radius). I want to convert the radius from inches to centimeters by multiplying it by 2.54. Then I want to calculate areas using the radius, one set as inches^2 and one set as centimeters^2. I don't know how to input values from the excel column into formulas on MatLab. I get the error that operator '*' is not supported for operands of type 'table', but when I forgo '*' I get another error stating "Invalid Experssion".
clc;
close all;
clear all;
% Prepare DataSet
Trans = readtable('Data1.xlsx');
radius = Trans(:,4)
%convert radius [inches] to radius [centimeters], 1 in = 2.54 cm
newradius = (radius)*2.54 %[cm]
%Calculate area in cm^3 and in^3, area = pi*r^2
area_in = pi*(radius)^2 %[in^2]
area_cm = pi*(newradius)^2 %[cm^2]
댓글 수: 0
채택된 답변
Chris
2023년 2월 6일
편집: Chris
2023년 2월 6일
The problem, as pointed out by Stephen, is that you are accessing the table data with parantheses, which creates another table. Your code will work if you use curly braces:
radius = Trans{:,4};
Here are some other options:
% Read in as a matrix, losing the variable names
Trans = readmatrix('Data1.xlsx')
radiusincm = Trans(:,4)*2.54
% Read in as a table, add new columns
Trans = readtable('Data1.xlsx');
Trans.radiusincm = 2.54*Trans.radius
% Read in as a table, refer to variable names
Trans = readtable('Data1.xlsx');
radiusincm = 2.54*Trans.radius
댓글 수: 2
Stephen23
2023년 2월 6일
"The problem is that you created a table called "radius" with a variable name "radius.""
The actual problem is that the OP used the wrong kind of indexing. To access table content use curly braces, not parentheses. The difference is explained in the MATLAB documentation:
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!