How import file csv with vertical header

조회 수: 18 (최근 30일)
Agnes Palit
Agnes Palit 2018년 7월 16일
답변: Walter Roberson 2018년 7월 22일
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
Walter Roberson 2018년 7월 22일
Please do not close a question that has an answer.

댓글을 달려면 로그인하십시오.

답변 (2개)

Christopher Wallace
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
Christopher Wallace
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
Agnes Palit 2018년 7월 16일
편집: Agnes Palit 2018년 7월 16일
Thank you, for the example data I already mention twice for you (above - that is the data I type manually which is actually put in csv file).
This is the following code:
filedir = '/Users/';
files1 = dir(fullfile(filedir, '*.csv'));
numFiles1 = length(files1);
for i = 1:numFiles1
if (contains(files1(i).name, 'header') == 1)
[header] = header(filedir, files1(i).name);
end
end
this is the function "header.m":
function [output] = header(folder, name)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
direction = strcat (folder, name);
opt = detectImportOptions(direction);
% opt = setvartype(opt, [2 9], 'datetime');
% opt = setvartype(opt, 4, 'double');
% opt = setvartype(opt, 5, 'double');
% opt = setvaropts(opt, [2 9], 'InputFormat', 'dd-MMM-yyyy', 'DatetimeFormat', 'eee uuuu-MM-dd HH:mm:ss' );
% opt = setvaropts(opt, 4,'Suffixes',' m')
% opt = setvaropts(opt, 5,'Suffixes',' kg')
% T = readtable(direction, opt);
T = readtable(direction, 'ReadVariableNames', false);
% T = T.';
newT = table2array(T(:,:));
newT = newT.';
newT = array2table(newT(:,:));
newT.Properties.VariableNames = {'Name', 'birth', 'age', 'height', 'weight', 'address', 'school', 'note', 'time'};
newT{2,1} = datetime(newT{2,1});
output = newT(:,:);
end

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2018년 7월 22일
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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by