How to import data To Matlab
Show older comments
Accepted Answer
More Answers (1)
OK, I had a little time once discovered there was code already there and not just starting from the description.
I recast initially into a function; using scripts is very dangerous as well as the use of the clear words wreaks havoc on existing workspace if doing anything at all else and so should be avoided when possible.
The reworked function through the nodes now works -- obviously renaming it to something more apropos to its purpose would be a starting detail <vbg>.
The first point where your code breaks is where the length of the string to be parsed was artifically limited to be 5000 characters; in the following line...
coord_section = s(index_coords:min(index_coords+5000, length(s)));
Instead, I added
ixEnd=index_coords+strfind(s(index_coords:end),' ==== DECOUPAGE TERMINE')-1; % find end of section -- probably better way but works
coord_section = s(index_coords:ixEnd); % only get to where want
that limits the search to the section itself and backs up to not pickup the trailing text.
Then, having the raw data of the file at hand, parse the numerical data directly...MATLAB works in column order (down looking a a 2D array), so to read the data as is written we must transpose the lines to be columns and then convert. Once converted, then transpose the output array back to having four columns.
% let's work on the raw char data directly instead...
ixStart=strfind(coord_section,newline)+1; % skip the header line
coord_nodes=reshape(sscanf(coord_section(ixStart:end).','%f'),4,[]).'; % convert to double, reshape
After doing the above, there's no need for converting the raw data into cellstr arrays and using the very slow str2num to parse.
That doesn't answer the problem above trying to run the initial code; that comes later in the following section --
% Create Mat_Coord_Elemt matrix
num_elements = size(Mat_top, 1);
Mat_Coord_Elemt = zeros(num_elements, 12); % Preallocate matrix
for i = 1:Mat_Coord_Elemt
That is clearly wrong; Mat_Coord_Elemt is the array; it's height() is num_elements from above so that loop range should be
for i = 1:num_elements % use height, not array name to iterate into
node1 = Mat_top(i, 1);
node2 = Mat_top(i, 2);
node3 = Mat_top(i, 3);
node4 = Mat_top(i, 4);
Mat_Coord_Elemt(i, :) = [
node1, node2, node3, node4, 0, ...
coord_nodes(node1, 2), coord_nodes(node1, 3), ...
coord_nodes(node2, 2), coord_nodes(node2, 3), ...
coord_nodes(node3, 2), coord_nodes(node3, 3), ...
coord_nodes(node4, 2), coord_nodes(node4, 3)
];
The above then still fails on the assignment line, however, with
Unable to perform assignment because the size of the left side is 1-by-12 and
the size of the right side is 1-by-13.
Error in readSomeUnknownPF3 (line 78)
Mat_Coord_Elemt(i, :) = [
which shows that there are 13 elements in the RH array but only room for 12. Reading the specification, the "0" is a separator of some function so the issue is the array isn't defined large enough to hold the needed 13 elements.
1 Comment
ADDENDUM:
There's a problem that the assignment of
node1 = Mat_top(i, 1);
node2 = Mat_top(i, 2);
node3 = Mat_top(i, 3);
node4 = Mat_top(i, 4);
Mat_Coord_Elemt(i, :) = [
node1, node2, node3, node4, 0, ...
coord_nodes(node1, 2), coord_nodes(node1, 3), ...
coord_nodes(node2, 2), coord_nodes(node2, 3), ...
coord_nodes(node3, 2), coord_nodes(node3, 3), ...
coord_nodes(node4, 2), coord_nodes(node4, 3)];
fails for quite a number owing to one or more of the nodes being 0 in Mat_top(i,1:4) and so are invalid array indices.
I don't know if this indicates a problem in Mat_top itself or whether the zero elements should be special-cased; you'll have to use the base code specification document to figure that out.
ADDENDUM
It is, in fact a problem in reading the data file -- as I suspected using the result of strfind as an index into importdata as a number of lines to skip is wrong -- strfind() is counting characters, not lines.
In this case, the result for the numeric topology data is
K>> topology_data.data(1:20,:)
ans =
1399 575 576 1400 4265 4214 4266 4259
1443 4 303 4 3 0 8 8
0 0 0 0 NaN NaN NaN NaN
1398 574 575 1399 4264 4191 4265 4257
1444 4 303 4 3 0 8 8
0 0 0 0 NaN NaN NaN NaN
1397 573 574 1398 4263 4168 4264 4255
1445 4 303 4 3 0 8 8
0 0 0 0 NaN NaN NaN NaN
113 3 573 1397 2568 4145 4263 4253
1446 4 303 4 3 0 8 8
0 0 0 0 NaN NaN NaN NaN
1396 1400 455 456 4260 4261 4262 4250
1447 4 303 4 3 0 8 8
0 0 0 0 NaN NaN NaN NaN
1395 1399 1400 1396 4258 4259 4260 4247
1448 4 303 4 3 0 8 8
0 0 0 0 NaN NaN NaN NaN
1394 1398 1399 1395 4256 4257 4258 4244
1449 4 303 4 3 0 8 8
K>>
whereas the actual data file content at that point is
K>> s(index_topology:index_topology+20*80)
ans =
'DESCRIPTEUR DE TOPOLOGIE DES ELEMENTS
1 4 303 1 3 0 8 8 0 0 0 0
2376 761 79 805 7026 6175 3452 7016
2 4 303 1 3 0 8 8 0 0 0 0
2375 762 761 2376 7025 6177 7026 7014
3 4 303 1 3 0 8 8 0 0 0 0
2374 763 762 2375 7024 6179 7025 7012
4 4 303 1 3 0 8 8 0 0 0 0
2373 764 763 2374 7023 6181 7024 7010
5 4 303 1 3 0 8 8 0 0 0 0
2372 765 764 2373 7022 6183 7023 7008
6 4 303 1 3 0 8 8 0 0 0 0
2371 766 765 2372 7021 6185 7022 7006
7 4 303 1 3 0 8 8 0 0 0 0
2370 767 766 2371 7020 6187 7021 7004
8 4 303 1 3 0 8 8 0 0 0 0
2369 768 767 2370 7019'
K>>
One needs to count lines, not characters. As noted before, this could be solved by reading the whole file as either a string array by line or a cell array by individual cells or by parsing the file content and reading directly.
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!