How to extract data from cell array

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 个评论

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

请先登录,再进行评论。

 采纳的回答

Stephen23
Stephen23 2020-5-13
编辑:Stephen23 2020-5-13
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 个评论

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)

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Cell Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by