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일

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

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