Clear Filters
Clear Filters

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

2 views (last 30 days)
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]

Accepted Answer

the cyclist
the cyclist on 11 Mar 2020
Edited: the cyclist on 11 Mar 2020
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 Comments
Damon Schmidt
Damon Schmidt on 11 Mar 2020
I was able to tweak my loop to account for the result being a cell array. Thanks again for the help.
Stephen23
Stephen23 on 12 Mar 2020
Edited: Stephen23 on 12 Mar 2020
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.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!