Convert cell array into matrix, but with removing the words, commas, etc

1 次查看(过去 30 天)
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 =
[]

采纳的回答

Stephen23
Stephen23 2020-3-2
编辑:Stephen23 2020-3-2
>> 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

更多回答(1 个)

Guillaume
Guillaume 2020-3-2
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)
  1 个评论
Damon Schmidt
Damon Schmidt 2020-3-2
It originates from a .txt file that has a lot of other information in it. I was able to filter through everything except this one array because of the words in the matrix. I see what your recommending though, thanks for the help!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by