Converting a cell array column of strings to a numeric matrix
2 次查看(过去 30 天)
显示 更早的评论
I am trying to manipulate a .csv file after importing it through a user created file exchange code called specifically: CSVIMPORT. The resulting data is an array, of which I am only concerned with one column. Being recognized as an array of strings, my intention is to convert the column to a simple 1x1 matrix. However, applying the cell2mat command to the column as a whole returns a concatenation error as follows:
??? Error using ==> cat CAT arguments dimensions are not consistent.
Error in ==> cell2mat at 89 m{n} = cat(1,c{:,n});
The column of interest has empty cells interspersed between a data sets of a single cell that would look as follows, for example:
'0|1.75|1|.5|0'
With that being said, the goal of this code would be to automate the process of copy and pasting that column to the workspace and separating the data with the import wizard with some variation of the delimiter.
Can you explain to me the best way to convert the data from the imported cell array form into its final delimited matrix fashion?
All the best.
2 个评论
Fangjun Jiang
2011-10-28
I would recommend you try csvread(), importdata() or xlsread() first to import your csv file. If neither can get you to the right format, please provide an example of the resulting data and explain what is your final expected output.
回答(1 个)
Fangjun Jiang
2011-10-31
If CSVIMPORT can't get what you want, I would start with your original .csv file and do proper parsing. Anyway, save your above example text data in test.csv and run the following code.
% the whole file is imported as cell array of strings
a=importdata('test.csv');
% read in all the non-whitespace characters, excluding the single quotes
b=regexp([a{:}],'[^''\s]*','match');
% pick the second column only
c=b(2:3:end);
% red the numerical data
d=regexp(c,'\|','split');
% make it numerical matrix
e=str2double(cat(1,d{:}));
>> whos e
Name Size Bytes Class Attributes
e 18x5 720 double
>> e
e =
1.3750 2.0625 2.7500 1.6250 1.5000
1.0000 0.5625 0.4375 0 0
0 0.5000 1.6875 2.1875 2.3125
1.4375 1.7500 2.0625 1.7500 1.0625
1.0625 1.1250 1.0625 1.0625 1.0625
1.0625 1.0625 1.0625 1.0625 1.1250
1.0625 1.0625 1.0625 1.0625 1.0625
1.0625 1.0625 0.1875 0 0
0 0.5000 1.6250 1.1875 0.3125
0.3750 0.3125 0.3750 0.3125 0.3750
0.3125 0.3750 0.5625 0.6875 0.6875
0.7500 0.6875 0.6875 0.6875 0.6875
0.8125 0.8125 0.8125 0.8750 0.8125
0.8125 0.8125 0.8125 0.8125 0.8125
0.8125 0.8125 0.8125 0.8125 0.8125
0.8125 0.8750 0.8125 0.8750 0.8750
0.8125 0.8125 0.8125 0.8750 0.8125
0.8125 0.8125 0.8750 0.8125 0.8125
2 个评论
Fangjun Jiang
2011-10-31
You may use different version of MATLAB than mine. As long as your text data contains similar strings as you show in your post, you can use the code above. Or, create the .csv file as above as I did and understand the code. Then you can apply the technique to your real .csv file with some tweaks.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!