How import file csv with vertical header
显示 更早的评论
I am trying to import file csv. The header of the file is located on the vertical layout. For example, there are table with dimention 9x2, and the header is in column 1.
Name :; A
birth :; 22-Feb-1964
Age :; 1
Height :; 168.8 m
Weight :; 50.0 kg
Address :; C
School :; D
Note :; E
Range of time :; 07-May-14 13:46 - 12-May-14 08:12
Any of you have an idea how to import this csv file, by aware the type of each data (e.g.: double/string/etc)?
1 个评论
Walter Roberson
2018-7-22
Please do not close a question that has an answer.
回答(2 个)
Christopher Wallace
2018-7-16
Hi Agnes,
One way you could accomplish this is to first convert your data into an array and then transpose it.
a = readtable('YourCSV.csv', 'Delimiter', ';', 'ReadVariableNames', false);
b= table2array(a)'; % convert to array and transpose.
Then you'll need to convert it back to a table and update the variable names.
newTable = array2table(b(2,:));
To update the variables names you can use the command:
newTable.Properties.VariableNames = a{:,1};
BUT... the way the names are currently recorded in the data results in invalid table variable names due to the space and colon at the end.
You could remove those in a loop prior to setting them.
7 个评论
Agnes Palit
2018-7-16
Christopher Wallace
2018-7-16
What do you want the data types to be?
Agnes Palit
2018-7-16
Christopher Wallace
2018-7-16
You can use various methods to adjust the type but it depends on the starting type and what you want it to be. Without an example of what you want to do the best I can tell you is to look at:
str2double() and
num2str
Agnes Palit
2018-7-16
编辑:Agnes Palit
2018-7-16
Christopher Wallace
2018-7-16
编辑:Christopher Wallace
2018-7-16
Please give me a specific example of what you're trying to do and all of the code you currently have.
Agnes Palit
2018-7-16
编辑:Agnes Palit
2018-7-16
Walter Roberson
2018-7-22
0 个投票
It is not possible to use readtable() for this purpose in any useful way, as readtable() expects the columns to be consistent data format, and would probably just give up and say that the second column was all character vectors.
You could perhaps adapt the code I posted in https://www.mathworks.com/matlabcentral/answers/285186-importing-data-without-knowing-number-of-columns#comment_368710 which goes through a bunch of trouble to figure out what the data types are of a cell array
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!