Help creating a homogenous array when one or more columns has data type 'cell'
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a data set in table format in which all columns have a single entry (type double) except for a few columns that have type cell (but still only a single entry).
I'm trying to manipulate this data, but before I do I need to reformat the 'cell' columns from type cell into type double to match the rest of the data. This process needs to be generic enough to convert the whole table into the same data format no matter which columns are which data type.
I was thinking about doing some loop like:
for i = 1:size(table)
column(i) = 'thing that converts a vector of cells into same size vector of doubles'
end
Any help is appreciated
0 Kommentare
Antworten (1)
dpb
am 25 Aug. 2020
I'm guessing here, but try
for i=1:width(t)
if iscell(t{:,i})
t.(t.Properties.VariableNames{2})=cell2mat(t{:,2});
end
end
where t is your table variable.
5 Kommentare
dpb
am 26 Aug. 2020
Bearbeitet: dpb
am 27 Aug. 2020
"When a single cell has a word, ... 'readtable' turns the whole column into data type 'cell'."
Yes...it really has no choice; a column has to be a homogenous type and since the string can't be converted, the option would be to not return the column at all or return it but as a cell array.
However, you should be in luck -- use the import options object with the 'ImportErrorRule','fill' and the 'FillValue','NaN'. This is the default combination but you have to use the import options object to bring it into play, readtable on its own doesn't go the extra mile.
opt=detectimportoption('yourfile.csv');
t=readtable('yourfile.csv',opt);
I just happened to have a suitable test file on hand from an Answers Q? of days of yore:
>> type ziad.txt
1 3 5 6 temp 56 d 3
4 f 45 5 6 H 4 5
>>
has some character data scattered around amongst the numeric values as does your file..
opt=detectImportOptions('ziad.txt');
t=readtable('ziad.txt',opt);
gives
>> t
t =
2×8 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8
____ ____ ____ ____ ____ ____ ____ ____
1 3 5 6 NaN 56 NaN 3
4 NaN 45 5 6 NaN 4 5
>>
where the string data locations are now NaN but all variables are numeric.
Moral: It pays to read the See Also section of the documentation! :)
ADDENDUM: Depending on the location and number of strings in the input file, it is possible that detectImportOptions will decide a given column is character data instead of numeric. This can/will happen if the first several records for the variable happen to be character data instead of numeric.
So, for generic solution, you will want to ensure the identified variable types are double for all columns in the import options object.
Also NB: you can create one of these import objects that matches the file format and save it as a .mat file and reload/reuse it for any file of the form...it will need to match the number of columns so if you have files with differing numbers of of variables, you'll need one consistent with the file. But, you don't have to call and cleanup the object for every file if you have a series of similar files.
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!