Reading a string & sorting it into usable variables

Hi!
I am working on a function that reads a .lpt file & then completes some computations using the values included.
This is what the test file structure looks like:
"
Layup: [45/-45/0/90/90/0/-45/45]
Ply thickness: [0.18/0.18/0.18/0.18/0.18/0.18/0.18/0.18]
Elastic properties: "IM7", 162000, 8340, 2070, 0.34
"
I can read the entire file in as a string. What is the best method to take that string & remove the vectors & properties from it? Essentially, I want to delete the "Layup:" & save the values in [ ] as a vector without the /. The lengths of the vectors can change too.
Thank you!

Answers (2)

Let's take a sample string.
S = "Layup: [45/-45/0/90/90/0/-45/45]";
Get the text after the leading [
S2 = extractAfter(S, "[")
S2 = "45/-45/0/90/90/0/-45/45]"
and before the trailing ].
S3 = extractBefore(S2, "]")
S3 = "45/-45/0/90/90/0/-45/45"
Now we could split S3 into pieces using the slash character as the delimiter then convert to double.
S4 = split(S3, "/")
S4 = 8×1 string array
"45" "-45" "0" "90" "90" "0" "-45" "45"
S4 = double(S4).'
S4 = 1×8
45 -45 0 90 90 0 -45 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Alternately, replace the / with space and use sscanf.
S5 = replace(S3, "/", " ")
S5 = "45 -45 0 90 90 0 -45 45"
S5 = sscanf(S5, "%d").'
S5 = 1×8
45 -45 0 90 90 0 -45 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
It's not clear how you want to handle the 'Elastic properties' line: is "IM7" data to be saved or 'header' / 'description' to be removed? I'm guessing data, but you can't have both text and numbers in a numeric array.
Create demo file:
writelines({'Layup: [45/-45/0/90/90/0/-45/45]','Ply thickness: [0.18/0.18/0.18/0.18/0.18/0.18/0.18/0.18]','Elastic properties: "IM7", 162000, 8340, 2070, 0.34'},'./mydata.ipt')
Check file content:
type ./mydata.ipt
Layup: [45/-45/0/90/90/0/-45/45] Ply thickness: [0.18/0.18/0.18/0.18/0.18/0.18/0.18/0.18] Elastic properties: "IM7", 162000, 8340, 2070, 0.34
Import file data as a table:
tbl = readtable('./mydata.ipt', 'FileType','text','ReadVariableNames',false, 'ReadRowNames',true, 'Delimiter',':');
tbl.Properties.VariableNames = {'Value'}
tbl = 3×1 table
Value _____________________________________________ Layup {'[45/-45/0/90/90/0/-45/45]' } Ply thickness {'[0.18/0.18/0.18/0.18/0.18/0.18/0.18/0.18]'} Elastic properties {'IM7, 162000, 8340, 2070, 0.34' }
Option 1: one non-scalar structure
nss = struct('Name',tbl.Properties.RowNames);
for k = 1:height(tbl)
[nss(k).Text,nss(k).Value] = parseval(tbl.Value{k});
end
display(nss)
nss = 3×1 struct array with fields:
Name Text Value
This you can access using a combination of structure indexing, e.g.:
nss(3).Name
ans = 'Elastic properties'
nss(3).Text
ans = 'IM7'
nss(1).Value
ans = 1×8
45 -45 0 90 90 0 -45 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Option 2: nested scalar structures
ss = struct();
for k = 1:height(tbl)
f = matlab.lang.makeValidName(tbl.Properties.RowNames{k});
[ss.(f).Text,ss.(f).Value] = parseval(tbl.Value{k});
end
display(ss)
ss = struct with fields:
Layup: [1×1 struct] PlyThickness: [1×1 struct] ElasticProperties: [1×1 struct]
This you can access using dot indexing into the nested structures, e.g.:
ss.ElasticProperties.Text
ans = 'IM7'
ss.Layup.Value
ans = 1×8
45 -45 0 90 90 0 -45 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Option 3: one table
for k = 1:height(tbl)
[tbl.Text{k},tbl.Value{k}] = parseval(tbl.Value{k});
end
display(tbl)
tbl = 3×2 table
Value Text ___________________________________________________________ __________ Layup {[ 45 -45 0 90 90 0 -45 45]} {0×0 char} Ply thickness {[0.1800 0.1800 0.1800 0.1800 0.1800 0.1800 0.1800 0.1800]} {0×0 char} Elastic properties {[ 162000 8340 2070 0.3400]} {'IM7' }
You could also transpose the table so that you can access the name using dot indexing:
tbl = rows2vars(tbl,'VariableNamingRule','modify');
tbl.Properties.RowNames = tbl.OriginalVariableNames; % this should be the default of ROWS2VARS
tbl = removevars(tbl,'OriginalVariableNames') % together with this.
tbl = 2×3 table
Layup PlyThickness ElasticProperties ___________________________ ___________________________________________________________ ___________________________ Value {[45 -45 0 90 90 0 -45 45]} {[0.1800 0.1800 0.1800 0.1800 0.1800 0.1800 0.1800 0.1800]} {[162000 8340 2070 0.3400]} Text {0×0 char } {0×0 char } {'IM7' }
Access the data using table indexing, e.g.:
tbl.ElasticProperties{2}
ans = 'IM7'
tbl.Layup{1}
ans = 1×8
45 -45 0 90 90 0 -45 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
tbl{"Value","Layup"}{1}
ans = 1×8
45 -45 0 90 90 0 -45 45
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Function which you can easily modify as required, to suit different data formats:
function [str,num] = parseval(txt)
[one,~,~,idx] = sscanf(txt,'[%f');
str = '';
if idx>1
num = [one,sscanf(txt(idx:end),'/%f',[1,Inf])];
else
[str,~,~,idx] = sscanf(txt,'%[^,]');
num = sscanf(txt(idx:end),',%f',[1,Inf]);
end
end

Categories

Asked:

on 19 Mar 2026 at 20:13

Edited:

about 12 hours ago

Community Treasure Hunt

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

Start Hunting!