Convert a cell array into matrix, but with removing the words / commas
Afficher commentaires plus anciens
So I'm trying to sort through the two arrays below, and place them in a matrix. The code I'm trying to use is also below. However, the outputs aren't giving me the desired result. I'm trying to take out the (known, known and free, known) for issue 1 and (plane stress) for issue 2, leaving just the numbers in the matrix. Any help on how to correct this would be greatly appreciated.
(Issue 1)
NF_array =
4×1 cell 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' }
NF_array = S(NF_2:NL_2);
optf = {'Delimiter',',', 'CollectOutput',true};
fmtf = ['%f%*s',repmat('%f',1,4)];
strf = sprintf('%s\n',NF_array{:});
outf = textscan(strf,fmtf,optf{:});
Nodal_Fixity = outf{1};
Nodal_Fixity = sortrows(Nodal_Fixity)
(Result from code) - Not Correct
Nodal_Fixity =
1 NaN NaN NaN NaN
(Need it to output this)
1 0 0
4 0 0
5 0 0
8 0 0
(Issue 2)
ED_array =
3×1 cell array
{'1, plane stress, 1, 2, 7, 8, 0.1, 29e6, 0.3, 7.3e-6'}
{'2, plane stress, 2, 3, 6, 7, 0.1, 29e6, 0.3, 7.3e-6'}
{'3, plane stress, 3, 4, 5, 6, 0.1, 29e6, 0.3, 7.3e-6'}
ED_array = [{'1, plane stress, 1, 2, 7, 8, 0.1, 29e6, 0.3, 7.3e-6'}, {'2, plane stress, 2, 3, 6, 7, 0.1, 29e6, 0.3, 7.3e-6'}, {'3, plane stress, 3, 4, 5, 6, 0.1, 29e6, 0.3, 7.3e-6'}];
ED_array = S(ED_2:EL_2);
opt = {'Delimiter',',', 'CollectOutput',true};
fmt = ['%f%*s',repmat('%f',1,8)];
str = sprintf('%s\n',ED_array{:});
out = textscan(str,fmt,opt{:});
Element_Data = out{1};
Element_Data = sortrows(Element_Data)
(Result from code)- Not Correct
Element_Data =
1.0e+07 *
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 2.9000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 2.9000 0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 2.9000 0.0000 0.0000
(Need it to output this)
1 1 2 7 8 0.1 29e6 0.3 7.3e-6
2 2 3 6 7 0.1 29e6 0.3 7.3e-6
3 3 4 5 6 0.1 29e6 0.3 7.3e-6
4 commentaires
Ameer Hamza
le 7 Mar 2020
Please edit your question and add code which we can directly execute in MATLAB. For example, how are these cell array created, and what is the name of the variable?
{'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' }
Damon Schmidt
le 7 Mar 2020
Ameer Hamza
le 7 Mar 2020
Yes, that makes it easy to understand the issue. But it better to format the code such that we can copy the code from here and paste it in MATLAB console. For example, a better way to put your code is
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' }];
Also i guess that the following line is not required
NF_array = S(NF_2:NL_2);
because NF_array is already defined.
Please also provide the output you want.
1 NaN NaN NaN NaN
is wrong output, so what is the actual output you want.
Damon Schmidt
le 7 Mar 2020
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Whos dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!