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 件のコメント

Sindar
Sindar 2020 年 5 月 13 日
Pelayo Vázquez Rodríguez
Pelayo Vázquez Rodríguez 2020 年 5 月 13 日
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 日

0 投票

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 件のコメント

Pelayo Vázquez Rodríguez
Pelayo Vázquez Rodríguez 2020 年 5 月 13 日
Thank you so much Stephen. Perfect
Pelayo Vázquez Rodríguez
Pelayo Vázquez Rodríguez 2020 年 5 月 14 日
¿How could I join a 3x1 cell into a single string?
Thank you Stephen
Stephen23
Stephen23 2020 年 5 月 14 日
Try these, where C is your cell array:
[C{:}]
sprintf(' %s',C{:})
join(C)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by