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

1 Ansicht (letzte 30 Tage)
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 =
[]

Akzeptierte Antwort

Stephen23
Stephen23 am 2 Mär. 2020
Bearbeitet: Stephen23 am 2 Mär. 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

Weitere Antworten (1)

Guillaume
Guillaume am 2 Mär. 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)
  1 Kommentar
Damon Schmidt
Damon Schmidt am 2 Mär. 2020
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by