Convert cell array into matrix, but with removing the words, commas, etc
1 view (last 30 days)
Show older comments
I have a cell array (ED_array). Shown below. I'm trying to filter out the thin fin portion and then convert it to a matrix with just numbers.
(Cell Array)
ED_array =
3×1 cell array
{'1, thin fin, 1, 2, 7, 8, 0.1, 400.0'}
{'2, thin fin, 2, 3, 6, 7, 0.1, 400.0'}
{'3, thin fin, 3, 4, 5, 6, 0.1, 400.0'}
I have been trying to use the code below, but my output (also below) just displays empty brackets because of the 'thin fin' column. I don't want or need the thin fin portion. 'Thin fin' can change to multiple names and the clolumns can increase to any amount, so the code needs to be universal to the input file. Any help would be greatly appreciated. Thanks.
(Code)
ED_array = S(ED_2:EF_2)
Element_Data = cell2mat(cellfun(@str2num,ED_array,'uniform',0))
(Output)
Element_Data =
[]
0 Comments
Accepted Answer
Stephen23
on 2 Mar 2020
Edited: Stephen23
on 2 Mar 2020
>> ED_array = {'1, thin fin, 1, 2, 7, 8, 0.1, 400.0';'2, thin fin, 2, 3, 6, 7, 0.1, 400.0';'3, thin fin, 3, 4, 5, 6, 0.1, 400.0'};
>> opt = {'Delimiter',',', 'CollectOutput',true};
>> fmt = ['%f%*s',repmat('%f',1,6)];
>> str = sprintf('%s\n',ED_array{:});
>> out = textscan(str,fmt,opt{:});
>> out = out{1}
out =
1 1 2 7 8 0.1 400
2 2 3 6 7 0.1 400
3 3 4 5 6 0.1 400
More Answers (1)
Guillaume
on 2 Mar 2020
Where does the cell array come from? If it's from a file import then the simplest thing is to fix the import. It is very likely that you could import the data in the exact format that you want with just a few lines of code.
Most likely something like:
filename = 'C:\somewhere\somefile.ext'
opts = detectImportOptions(filename);
opts.SelectedVariableNames = opts.VariableNames([1, 3:end]); %tell matlab to skip 2nd variable
Element_Data = readmatrix(filename, opts)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!