Reading a string & sorting it into usable variables
Mostrar comentarios más antiguos
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!
Respuestas (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, "[")
and before the trailing ].
S3 = extractBefore(S2, "]")
Now we could split S3 into pieces using the slash character as the delimiter then convert to double.
S4 = split(S3, "/")
S4 = double(S4).'
Alternately, replace the / with space and use sscanf.
S5 = replace(S3, "/", " ")
S5 = sscanf(S5, "%d").'
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
Import file data as a table:
tbl = readtable('./mydata.ipt', 'FileType','text','ReadVariableNames',false, 'ReadRowNames',true, 'Delimiter',':');
tbl.Properties.VariableNames = {'Value'}
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)
This you can access using a combination of structure indexing, e.g.:
nss(3).Name
nss(3).Text
nss(1).Value
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)
This you can access using dot indexing into the nested structures, e.g.:
ss.ElasticProperties.Text
ss.Layup.Value
Option 3: one table
for k = 1:height(tbl)
[tbl.Text{k},tbl.Value{k}] = parseval(tbl.Value{k});
end
display(tbl)
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.
Access the data using table indexing, e.g.:
tbl.ElasticProperties{2}
tbl.Layup{1}
tbl{"Value","Layup"}{1}
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
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!