How to use dot notation (substructures and fields) with a parameter which has already "dot" in it??

73 visualizaciones (últimos 30 días)
Hello everyone I am new at matlab, so I think this is a very basic thing but I couldn't solve this.
I have 1x1 cells that contains field names(which they are also 1x1 substruct) of the 1x1 structure. I want to access these substructs and "their" fields with 1x1 cells, because I don't want to write all the field names one by one; I get the cells with get_param function, and I intend to use them with getfield function.
For example; Let's say main structure is " EXC " and substructure is " abc " and I want to get " data " field of abc substructure. I have " abc " field name as a 1x1 cell let's say " conS ". So I write this and I can get the data without problem;
getfield(EXC.(conS{1}), 'data')
ans =
1 0 (for example)
no problem as far but problem begins with when field name, named with also "dot" like " EXC. def ". Let's say this 1x1 cell " conR". When I use cells that have field names starting with " EXC.~~" the code does'nt work, gives this error;
getfield(EXC.(conR{1}), 'data')
Reference to non-existent field 'EXC.def'.
But when I write " EXC.def.data ", I can acces to data...
EXC.def.data
ans =
1 2 (for example)
Normally I have nx1 cells that have field names and some of them have "EXC." at their front and some of them don't have. But I need to use all of them with getfield ( or some other function) in a for loop to get their data one by one.
How can I do this? How can I use these cells that have "EXC.~~" with getfield function? or are there any other function I can use to do all of this, I really appreciate your help as I am beginner of MATLAB.
Thanks in advance, I hope I made it clear, you can ask anything that you couldn't figure out.

Respuesta aceptada

dpb
dpb el 15 de Oct. de 2017
Short answer is "you can't do that!". The field name in the variable has to be a single field and a field name can't have an embedded dot within it. It's just illegal syntax and there is no way around that that is anything will recommend as a solution (even to a veteran user and certainly to a novice, anyway).
The solution is to remove the dot from the cell; if you're really trying to read an existing field but that is a field of the first such that there are two dots to dereference it as your working text example from command line, then store those as separate variables; don't try to combine them.
I'd suggest it may be you're nesting too deeply if you're running into this problem or perhaps trying to store the data in other overly-complex manner that there's a much easier solution but we can't tell much about that without more background on what you actually started with and what you're trying to accomplish.
  10 comentarios
dpb
dpb el 15 de Oct. de 2017
"Undefined function 'strsplit' for input arguments of type 'cell'."
You have to dereference the cellstr content with the curlies...
dpb
dpb el 15 de Oct. de 2017
if isfield(EXC, conR) == 0
xlswrite('excstj.xlsx', conR)
elseif isempty(EXC.(conR{1}).data) == 1
xlswrite('excstj.xlsx', conR)
end
Don't know the purpose here, but the two condition tests don't really do anything since the code is the same either way inside...perhaps you're trying to not do anything if both are false???
if ~isfield(EXC, conR) | isempty(EXC.(conR{1}).data)
xlswrite('excstj.xlsx', conR)
end
NB: Don't need the specific '==Value' for a logical test; the condition is True if ~0 and False if 0. The result of
if condition==1
is simply
if _value_
where value is the result of the expression condition==1 which just reduces to either 0 or 1 anyway. The ==0|1 is just superfluous. Only if you were comparing a value to a specific choice out of a set of alternatives such as 1 to N would it be necessary (which could like be better written as a SWITCH block anyways).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programmatic Model Editing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by