How do I convert table data to double to manipulate them?
1,784 次查看(过去 30 天)
显示 更早的评论
I extract data from a table. When I retrieve an element, it is in table format, but I encounter an error when attempting to perform operations like multiplication. Is there a direct command or a series of steps to address this issue?
3 个评论
Stephen23
2021-8-25
编辑:Stephen23
2021-8-25
"I extracted data from a table, when I get an element it comes as table format but when trying to make some operations like multiplication there is an error, is there any direct command or a series of steps?"
You could use TABLE2ARRAY, but by far the simplest and most efficient approach is to just use the correct indexing using curly braces to access the table content (not parentheses which returns another table, which is what you did).
The different types of indexing are clearly explained in the documentation:
Peter Perkins
2021-11-8
This new example goes even further into indexing and the topic of "doing math on data stored in a MATLAB table":
It's a new-for-R2021b example, but doesn't require the latest MATLAB for anything (most anything?) it covers.
采纳的回答
Star Strider
2024-9-4
编辑:MathWorks Support Team
2024-6-5
There are several ways you can convert or extract table data to an array.
You can use dot notation or curly brace indexing, as described in the following documentation:
Access Data in Tables
https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html
A = T.Variables% All table data concatenated into an array
A = T{:,:}% All table data concatenated into an array
A = T{rows,vars}% Specified rows and variables into an array
You can also use the "table2array" function.
A = table2array(T)
And finally, there are many operations you can perform on table data without converting it at all. For examples, see the following documentation:
Direct Calculations on Tables and Timetables
https://www.mathworks.com/help/matlab/matlab_prog/direct-calculations-on-tables-and-timetables.html
Calculations When Tables Have Both Numeric and Nonnumeric Data
https://www.mathworks.com/help/matlab/matlab_prog/calculations-when-tables-have-both-numeric-and-nonnumeric-data.html
5 个评论
Peter Perkins
2023-9-28
In recent versions of MATLAB, tables and timetables support many element+wise and reduction math operations:
t1 = table([1;2;3],[4;5;6]);
sum(t1,1)
t2 = table([7;8;9],[10;11;12]);
t1 + t2
For many other operations, see https://www.mathworks.com/help/matlab/matlab_prog/computations-with-numeric-data-in-table-or-timetable.html.
更多回答(3 个)
Peter Perkins
2017-12-19
The answer might be as simple as something like T.Z = T.x .* T.y. Without more information, hard to tell.
0 个评论
Roger Breton
2021-3-8
编辑:Roger Breton
2021-3-8
It does not work for me : all I want is to be able to import a range from Excel, and all I get is a "Table" on which I can't do any operation?
3 个评论
Roger Breton
2021-3-9
Thank you so much for extending your kind help, Peter.
Yes, all my data is numieric, doubles, in fact. It's just that I'm totally "green" with regards to MatLab data types. I think I used the table2array function to "convert" the data. Once converted, I was able to carry my "normal" arithmetic operation. I really have to give myself the time to learn Matlab...
Arshey Dhangekar
2021-7-7
Hello I have csv data of 18 columns and want to convert into double
so instead of writing 18 str2double code how can I convert all the 18 columns in short line of code
Temp.x310_Ambient__C_ = str2double (Temp.x310_Ambient__C_);
3 个评论
Mohsin Munir
2021-8-25
I have data in excel. there are some coulmns having inputs. I want if i multiply two columns in matlab then the next column to be added in the same sheet rather than a new file in workspace. can anybody help me in this problem
Peter Perkins
2021-11-8
1) Post a new question, don't ask a question in a reply to someone else's question.
2) This example may be of help for the topic of "doing math on data stored in a MATLAB table":
It's a new-for-R2021b example, but doesn't require the latest MATLAB for anything (most anything?) it covers.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!