Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
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 Kommentare
Ameer Hamza
Ameer Hamza am 7 Mär. 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
Damon Schmidt am 7 Mär. 2020
I just updated what my desired result is. The NF_array = S(NF_2:NL_2); and ED_array = S(ED_2:EL_2); are needed because I'm filtering the data from a larger array.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 7 Mär. 2020
Bearbeitet: Stephen23 am 7 Mär. 2020
In both cases it looks like you are processing data imported from a file, in which case it would be much more efficient to fix the data importing rather than inefficiently import everything as character and then convert to numeric.
NF_array
Your textscan format string does not match the data itself. For some reason you defined the format string with 1 numeric field, 1 string field, and 4 numeric fields. The actual data has 1 numeric field, 2 string fields, and 2 numeric fields. When I wrote the format string to correctly match the data, it worked without error:
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'};
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}
Giving:
Nodal_Fixity =
1 0 0
8 0 0
4 0 0
5 0 0
ED_array
You write that the output is "Not Correct", but in fact it is perfectly correct, you are not taking into account the limited display precision of the default short display format and this multiplication factor:
Element_Data =
1.0e+07 * <- !!!!!!!! DO NOT IGNORE THIS !!!!!!!!
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
If you display the value 1 and the value 29e6 together in one matrix which displays five digits with fixed precision based on the magnitude of the largest value in the matrix, then you will see that table. It does NOT mean that MATLAB has eaten your values, just that you need to learn about different ways that numbers can be displayed (which does NOT change what numbers are stored in memory).
You can easily change the format to something like shortG and you will see each element displayed as you probably expect to see them.
>> format short
>> Element_Data
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
>> format shortG
>> Element_Data
Element_Data =
1 1 2 7 8 0.1 2.9e+07 0.3 7.3e-06
2 2 3 6 7 0.1 2.9e+07 0.3 7.3e-06
3 3 4 5 6 0.1 2.9e+07 0.3 7.3e-06
  1 Kommentar
Damon Schmidt
Damon Schmidt am 7 Mär. 2020
I honestly wish I had you as my Matlab teacher during my undergrad class. Thanks so much for the help. The goal is to one day be as proficient as you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures 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