필터 지우기
필터 지우기

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

조회 수: 3 (최근 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에서 Creating 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