How do I split this data into different column and write it back in a text file with an extention .bdf?

I would like to split this data into different columns without any emoty cells and write it into a text file and save it as .bdf. Can you helps me ?
MAT1,88813,210000.0,,0.3,7.85e-09,,,,
MAT1,88815,210000.0,,0.3,7.85e-09,,,,
MAT1,88816,210000.0,,0.3,7.85e-09,,,,
MAT1,88817,210000.0,,0.3,7.85e-09,,,,
MAT1,88818,210000.0,,0.3,7.85e-09,,,,

 Accepted Answer

data=readlines('mat.fem');
M=split(data(startsWith(data,'MAT1'),:),',');
M(:,4)=num2str(rand(size(M,1),1));
modifies MAT1 as per request...
Now, we're still at a loss as to what to write -- you complain you can't remove empty columns, but say to write back in "the same original format" which would be comma-delimited with trailing empty fields.
So, which is it???
LATER RESUMPTION AFTER REFLECTION ON LIKELY SCENARIOS
OK, so presuming to take the "original" at face value, then next step is to replace the MAT1 section in the overall array and output the updated array --
M=join(M,','); % put back into csv form by line
ix=startsWith(data,'MAT1'); % locate in original array
data(ix)=M; % and replace with new/revised set
writematrix(data,'mat1.fem') % and write out
NB: I changed filename in last to not overwirte the original; salt to suit.
A minor efficiency improvement would be to compute the index vector ix first and use it to select M in the second line above as well -- saves doing same thing twice't as is in above as given.

6 Comments

@dpb I actually wanted to change the third column of MAT1 with random numbers. I changed the code accordindly.
Everything is working fine now just as I wanted but I am getting an error while writing the .fem file as
Error "Unrecognized file extension '.fem'. Use the 'FileType' parameter to specify the file type"
Also, while writing it in a .txt file, there are double quotes written for each and every line which is not the same as the original text.
M=split(data(startsWith(data,'MAT1'),:),',');
M=M'
M(:,3)=num2str(rand(size(M,1),1));
M=join(M,','); % put back into csv form by line
ix=startsWith(data,'MAT1'); % locate in original array
data(ix)=M; % and replace with new/revised set
writematrix(data,'mat1.fem') % and write out
Read the doc...and follow the error message instructions... :)
I tend to forget that the writeXXX family is fairly brain-dead -- they don't have enough smarts built in to just write text files for unrecognized extensions, the set of which are defined being pretty small. So, do as it says...
writematrix(data,'mat1.fem','filetype','text','quotestrings',false)
to also ensure not quoted strings. Not supposed to be by default but looks like that's tied in with not recognizing the text file format or something. I'll put in a bug report on quality of implementation/inconsistency with documentation on the issue.
But, moral of story is to dig into the doc -- it has the answers to most Q? w/o waiting for us.
If this completes this topic, go ahead and indicate so for rest by accepting the answer...then the community knows is a solution...
@dpb Thank you so much for your help.
But I seem to have one more error here with the above code.
Error "The 'QuoteStrings' parameter must be logical 1 (true) or logical 0 (false)."
So do as it says (not as I do) :)
I inadvertently quoted the keyword/function false...
To quote above...
"moral of story is to dig into the doc -- it has the answers to most Q? w/o waiting for us"

Sign in to comment.

More Answers (3)

So, what is this -- the content of some file, an array of text, a cell array... ??
What are we looking at here?
If it's a text file, simply
data=readcell('yourInputFile.ext');
data(:,all(~ismissing(string(data))));
will provide a cell array of the content w/o missing column(s).
Now, what is a ".bdf" file format to write???
ADDENDUM:
An alternative is to not bring in the empty cells to begin with -- use the import options object:
opt=detectImportOptions('addie.txt','MissingRule','omitvar');
data=readcell('yourInputFile.ext');
and your data array won't have missing variables in the first place.
We're still awaiting word on the output format to finish up...

4 Comments

Hi @dpb, a bdf file is a nastran input deck. (in full a bulk data file).
The MAT command defines the material properties for a finite element simulation. In this particular case, MAT1 indicates an isotropic material. 88813 is the material identifier, 210000 is the youngs modulus, 0.3 is the possion value and 7.84e-9 is the density.
That's well and good; but the Q? is what's the format for the file?
My recollection from years gone by is that they were fixed-width; has that restriction been lifted in modern era? (My consulting in the arena when I was interacting with those using NASTRAN ended 30+ years ago.)
A quickie search found a blog that says
"A field is an 8 character cell... Sixteen character fields exist, but this is a discussion for another time. Each line is at most 80 characters in length."
which would indicate need to use fixed-width fields yet. In that case, default formatting will NOT write a usable file.
the answers and comments has grown a bit, sorry if this is outdated :)
you are correct: the typical nastran input format is a fixed field format, where each field is made from 8 characters, the large field format uses the same princple but with 16 characters. And the free field format seperates the variables using comma's.
Following the standard field format (8 characters per variable), the mat1 line should look like
MAT1 888132.1000+5 0.3000007.8500-9
@dpb This is not working in my case. The data is not seperated into different columns. Thank you

Sign in to comment.

Hi @Adeline War, assumig you have the data in a string array, you can use the procedure below.
MyData = ["MAT1,88813,210000.0,,0.3,7.85e-09,,,,"
"MAT1,88815,210000.0,,0.3,7.85e-09,,,,"
"MAT1,88816,210000.0,,0.3,7.85e-09,,,,"
"MAT1,88817,210000.0,,0.3,7.85e-09,,,,"
"MAT1,88818,210000.0,,0.3,7.85e-09,,,,"]
MyData = 5×1 string array
"MAT1,88813,210000.0,,0.3,7.85e-09,,,," "MAT1,88815,210000.0,,0.3,7.85e-09,,,," "MAT1,88816,210000.0,,0.3,7.85e-09,,,," "MAT1,88817,210000.0,,0.3,7.85e-09,,,," "MAT1,88818,210000.0,,0.3,7.85e-09,,,,"
% replace the comma
MyData = strrep(MyData,","," ")
MyData = 5×1 string array
"MAT1 88813 210000.0 0.3 7.85e-09 " "MAT1 88815 210000.0 0.3 7.85e-09 " "MAT1 88816 210000.0 0.3 7.85e-09 " "MAT1 88817 210000.0 0.3 7.85e-09 " "MAT1 88818 210000.0 0.3 7.85e-09 "
% delete double spaces
MyData = regexprep(MyData,' +',' ')
MyData = 5×1 string array
"MAT1 88813 210000.0 0.3 7.85e-09 " "MAT1 88815 210000.0 0.3 7.85e-09 " "MAT1 88816 210000.0 0.3 7.85e-09 " "MAT1 88817 210000.0 0.3 7.85e-09 " "MAT1 88818 210000.0 0.3 7.85e-09 "
% print the data to .bdf file
MyFile = fopen('test.bdf','w+');
fprintf(MyFile,'%s\n',MyData);
fclose(MyFile);

2 Comments

@Adeline War Then see my Answer on how to remove the empty cells -- that's what the second line gives you starting with a cell array.
The real Q? still is the one about what is the required output format of the .bdf file?

Sign in to comment.

clc;
clear;
close;
[fp2 pn2] = uigetfile('mat.fem');
cd(pn2);
fileID = fopen(fp2);
C = textscan(fileID,'%s','Delimiter','\n');
a = C{1,1};
dm1=find(strncmp(a,'CTETRA',5));
dm2=(a(dm1));
dm4 = split(dm2,',');
mat = find(strncmp(a,'MAT1',3));
mat_1 = (a(mat));
F = split(mat_1,',');
I have attached here the code and file. I am unable to split MAT1 with comma delimeters and without empty column. Also, could we update 3rd column of MAT1 with any new random numbers and paste all this data in the same original format and save it as a .fem file. The only changes has to be for the MAT1 data.

2 Comments

Don't use Answer for comments/amplifications...either edit the original Q? or add comments.
I already showed you two ways to get rid of the empty columns...and you've introduced another file extension without addressing the Q? of the format of the first. Let's deal with one thing at a time.
Of course one can modify any data content at will, but let's deal with one topic at a time instead of randomly.
C = textscan(fileID,'%s','Delimiter','\n');
is archaic and worst possible way (other than, perhaps, impordata) to approach.
What release of MATLAB are you using? Unless it's very old and you can't update; this is NOT the way to write new code (and given the little code that is here that is optimal, there's no real good reason to keep this and not rewrite it more effeciently.)

Sign in to comment.

Asked:

on 5 Jul 2022

Commented:

dpb
on 9 Jul 2022

Community Treasure Hunt

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

Start Hunting!