フィルターのクリア

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

3 ビュー (過去 30 日間)
Damon Schmidt
Damon Schmidt 2020 年 3 月 2 日
コメント済み: Damon Schmidt 2020 年 3 月 2 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by