Convert a cell array into matrix, but with just removing the commas (Need words to stay)

2 次查看(过去 30 天)
Okay, so it turns out that I need to keep 'known' and 'free', but I can't seem to get this to work out for me. I'm still filtering through the same NF_array (which can be changed based on user input), but now I'm trying to keep the known and free in the matrix. Below is what I'm trying to do... Thanks for the help.
NF_array = [{'1, known, known, 0.0, 0.0'}, ...
{'8, known, known, 0.0, 0.0'}, ...
{'4, free, known, 0.0, 0.0' }, ...
{'5, free, known, 0.0, 0.0' }];
%Code I'm trying to use
syms known free %defining known and free in the program
NF_array = S(NF_2:NL_2);
optf = {'Delimiter',',', 'CollectOutput',true};
fmtf = '%f%s%s%f%f';
strf = sprintf('%s\n',NF_array{:});
outf = textscan(strf,fmtf,optf{:});
Nodal_Fixity = outf{1};
Nodal_Fixity = sortrows(Nodal_Fixity)
%What I'm getting... So I need columns 2:5 to populate still
Nodal_Fixity =
1
4
5
8
%(Need the code to process NF_array and convert to)%
Nodal_Fixity =
[ 1, known, known, 0, 0]
[ 4, known, known, 0, 0]
[ 5, free, known, 0, 0]
[ 8, free, known, 0, 0]

采纳的回答

the cyclist
the cyclist 2020-3-11
编辑:the cyclist 2020-3-11
NF_array = [{'1, known, known, 0.0, 0.0'}, ...
{'8, known, known, 0.0, 0.0'}, ...
{'4, free, known, 0.0, 0.0' }, ...
{'5, free, known, 0.0, 0.0' }];
Nodal_Fixity = cell(4,5);
for nr = 1:4
Nodal_Fixity(nr,:) = strsplit(NF_array{nr},',');
end
If you also need to remove the spaces, then follow that with
Nodal_Fixity = regexprep(Nodal_Fixity,' ','');
That does not need to be inside the loop; it will operate on the entire cell array.
  4 个评论
Stephen23
Stephen23 2020-3-12
编辑:Stephen23 2020-3-12
Note:
  • importing/storing numeric data as character/string and then converting it to numeric afterwards is going to be less efficient than importing it as numeric in the first place.
  • Storing numeric data as scalar arrays in a cell array is much less efficient than storing it in a simple numeric array.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by