Hellow,
I´m a aerospace Engineer,
I´ve this 4x1 cell array read from a text file by "fgetl" function. This code contains information about two nodes (B10 and D22) of a thermal FEM:
%%%%%%
B10 = 'cryos', T = T_caja_top,
A = 0.916088, ALP = 1.000000, EPS = 0.850000,
FX = -0.270000, FY = 0.000000, FZ = 0.000000;
D22 = 'Heater wire 1', T = T_INI, QI = PJoule;
%%%%%%
So, I would like to extrat all the information and create a tipe "structure" variable called "Nodes". Example:
Node B10, Atributes: Name:'cryos', Temperature = T_caja_top, Area = 0,916088...
Node D22, Atributes: Name: 'Heater wire 1', Temperature = T_INI....
Any help is really welcome.
Regard, the delimiter between nodes is: ';'
Thank you so much,
Pelayo Vázquez Rodríguez

2 commentaires

No. I’ve already checked it.
It is not so easy. Data name and values are mixed at the same cell

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 13 Mai 2020
Modifié(e) : Stephen23 le 13 Mai 2020
Because you did not upload a sample file I created one (attached) based on your example data.
str = fileread('trial.txt'); % read the entire file as one string
spl = regexp(str,'\w+[^;]+','match'); % split nodes
tkn = regexp(spl,'(\w+)\s*=\s*([^,]+)','tokens'); % identify key=value pairs
num = numel(tkn);
out = struct();
for k = 1:num
tmp = tkn{k};
tmp = strrep(vertcat(tmp{:}).','''','');
vec = str2double(tmp(2,:)); % optional: convert to numeric
idx = ~isnan(vec); % optional: convert to numeric
tmp(2,idx) = num2cell(vec(idx)); % optional: convert to numeric
out.(tmp{1,1}) = struct('Name',tmp{2,1},tmp{:,2:end});
end
Giving a scalar structure containing nested scalar structures with different fields:
>> out
out =
B10: [1x1 struct]
D22: [1x1 struct]
>> out.B10
ans =
Name: 'cryos'
T: 'T_caja_top'
A: 0.91609
ALP: 1
EPS: 0.85
FX: -0.27
FY: 0
FZ: 0
>> out.D22
ans =
Name: 'Heater wire 1'
T: 'T_INI'
QI: 'PJoule'

3 commentaires

Thank you so much Stephen. Perfect
¿How could I join a 3x1 cell into a single string?
Thank you Stephen
Try these, where C is your cell array:
[C{:}]
sprintf(' %s',C{:})
join(C)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by