필터 지우기
필터 지우기

Create Structure from cell array with dot separation in string

조회 수: 11 (최근 30일)
Philipp Pasolli
Philipp Pasolli 2022년 6월 30일
답변: Steven Lord 2022년 6월 30일
Hi everyone,
I am reading in data from a measurements where the signal names are imported as a 1-dimensional cell array. The respective strings in the cell array represent the signal name. The signal names have been given already an inherent "structure" themselves.
What i want to do now is to automatically create a structure in matlab based on the signal names and then associate the respective measurements which per signal are a 1-dimensional numeric array.
To clarify and give an example, two strings would be for example:
{'em1500.eta_t.x []' }
{'em1500.eta_t.y []' }
I now want to automatically create a structure like:
measurements.em1500.eta_t.x = (The measurements taken);
measurements.em1500.eta_t.y = (The measurements taken);
Does anybody has an idea how to automate that process?
Best regards
  댓글 수: 2
Stephen23
Stephen23 2022년 6월 30일
편집: Stephen23 2022년 6월 30일
"Does anybody has an idea how to automate that process?"
Creation of invalid fieldnames cannot be "automated".
However rather than writing fragile/buggy code that forces meta-data into fieldnames, why not just simply store that meta-data as data in its own right? Then your code will be simpler, more robust, and much more efficient.
Philipp Pasolli
Philipp Pasolli 2022년 6월 30일
Maybe my intention was not quite clear enough, if that is the case sorry.
measurements.em1500.eta_t.x = (The measurements taken);
In the scenario above, measurements would be the variable name of the structure. Then em1500 would be a fieldname of measurements, eta_t would be a fieldname within em1500 and x would be a fieldname within eta_t. Meaning that the structure ends up having three layers in that case if i am not wrong?
Since there is a lot of data from different "parts", to group the respective measurements in a logical ordere would make a lot of sense in my opinion, which is why i try to accomplish this in the first place.
Hope that explanation helped?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2022년 6월 30일
편집: Stephen23 2022년 6월 30일
You can use SETFIELD() with a comma-separated list:
V = pi;
T = 'em1500.eta_t.x';
C = split(T,'.');
S = struct();
S = struct with no fields.
S = setfield(S,C{:},V)
S = struct with fields:
em1500: [1×1 struct]
Checking:
S.em1500.eta_t.x
ans = 3.1416

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2022년 6월 30일
편집: Fangjun Jiang 2022년 6월 30일
If your question comes down to whether "em1500.eta_t.x" can be made a field name, then the anser is No. It is an invalid field name, just as you can't make "123 xyz" a variable name.
If you can make that name to be "em1500_eta_t_x", then you can use
MyStuct=struct('em1500_eta_t_x',rand)
MyStuct = struct with fields:
em1500_eta_t_x: 0.1513
To establish a multi-layer structure, you can do it directly
clear a
a=struct
a = struct with no fields.
a.b.c.d=1
a = struct with fields:
b: [1×1 struct]
a.b.c.d
ans = 1
  댓글 수: 1
Philipp Pasolli
Philipp Pasolli 2022년 6월 30일
편집: Philipp Pasolli 2022년 6월 30일
Thanks for the reply, but i dont think that is what i am looking for. I might have been not clear in my description.

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2022년 6월 30일
s = 'em1500.eta_t.x';
f = split(s, '.')
f = 3×1 cell array
{'em1500'} {'eta_t' } {'x' }
At this point you may want to use matlab.lang.makeValidName to ensure the field names (the 'pieces' of s) are valid identifiers.
mydata = setfield(struct, f{:}, 1)
mydata = struct with fields:
em1500: [1×1 struct]
mydata.em1500
ans = struct with fields:
eta_t: [1×1 struct]
mydata.em1500.eta_t
ans = struct with fields:
x: 1
Alternately you might want to make this a table array with s as either the row name or (if you're using a release that supports table variable names that aren't valid identifiers) the variable name.
t1 = table(1, 'VariableNames', {s})
t1 = table
em1500.eta_t.x ______________ 1
y = t1.("em1500.eta_t.x")
y = 1
t2 = table(1, 'RowNames', {s})
t2 = table
Var1 ____ em1500.eta_t.x 1
z = t2{"em1500.eta_t.x", 1}
z = 1

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by