# use struct in loop for

1 次查看（过去 30 天）
sedigheh Najafi 2024-3-27

Hi, for this code folowing error is existed. how can I solve this error? Thank you for your help.
s=[];
a=2;
b=1;
c=-3;
for i= 1 : 4
A = 'A'+num2str(j);
value = {a,b,c};
s = [s;struct(A,value)];
end
Error using struct
Field names must be non-empty character vectors or string scalars.
Error using struct
Field names must be non-empty character vectors or string scalars.
##### 4 个评论显示 2更早的评论隐藏 2更早的评论
sedigheh Najafi 2024-3-27

I have mistake in my code about j, code is this:
s=[];
a=2;
b=1;
c=-3;
for i= 1 : 30
A = 'A'+num2str(i);
value = {a,b,c};
s = [s;struct(A,value)];
end
Error using struct
Field names must be non-empty character vectors or string scalars.
I am trying to creat a struct with these fieldnames: A1, A2, ..., A30 with value = {a,b,c}.
Because there are a lot of fieldnames, I used the loop and I got this error.
Dyuman Joshi 2024-3-27

Reiterating Stephen's point - "using actual indexing is generally simpler and more efficient than forcing pseudo-indices into fieldnames."
Here's how you can use indices to store the data and access it accordingly (Rik has already shown this below) -
%% Storing the data using indices
%Reverse loop, so that it pre-allocates automatically
for k=30:-1:1
%I don't see a need to store them in a cell array, store the values in a numeric array
%storing the iteration number as an example
value = [k k k];
s(k).A = value;
end
%% Access it using indices
s(1).A
ans = 1x3
1 1 1
s(2).A
ans = 1x3
2 2 2

### 回答（2 个）

Rik 2024-3-27
I fully agree with Dyuman and Stephen.
As to the source of the error in this code: the error message is giving you a hint. Something is wrong with the field name. What you are doing is adding num2str(j) to 'A'.
Since you didn't assign a value to j, Matlab will assume you meant the imaginary unit (just like it would with i). That is why you should avoid those two as variable names.
The next step is the addition. Additions on char vectors work just like normal array operations. If you wanted a concatenation, that syntax works for strings (which use the double quotes " as definition).
This is my guess for what you intended to do:
s=struct;
a=2;
b=1;
c=-3;
for n = 1 : 4
value = {a,b,c};
s(n).A = value;
end
##### 2 个评论显示 无隐藏 无
sedigheh Najafi 2024-3-27

I have mistake in my code about j, code is this:
s=[];
a=2;
b=1;
c=-3;
for i= 1 : 30
A = 'A'+num2str(i);
value = {a,b,c};
s = [s;struct(A,value)];
end
Error using struct
Field names must be non-empty character vectors or string scalars.
I am trying to creat a struct with these fieldnames: A1, A2, ..., A30 with value = {a,b,c}.
Because there are a lot of fieldnames, I used the loop and I got this error.
Rik 2024-3-27
You should not want to do that. What exactly do you want to achieve?
Also, when posting a comment (or question or answer), please use the tools to format your post.

sai charan sampara 2024-3-27
Hello Najafi,
I understand that you are trying to create a struct "s" with field names "A1", "A2”, ... "A30" each having value of "{a,b,c}". It can be done as follows:
s=[];
a=2;
b=1;
c=-3;
for i= 1 : 30
value = {a,b,c};
s.("A"+num2str(i))=value;
end
s
s = struct with fields:
A1: {[2] [1] [-3]} A2: {[2] [1] [-3]} A3: {[2] [1] [-3]} A4: {[2] [1] [-3]} A5: {[2] [1] [-3]} A6: {[2] [1] [-3]} A7: {[2] [1] [-3]} A8: {[2] [1] [-3]} A9: {[2] [1] [-3]} A10: {[2] [1] [-3]} A11: {[2] [1] [-3]} A12: {[2] [1] [-3]} A13: {[2] [1] [-3]} A14: {[2] [1] [-3]} A15: {[2] [1] [-3]} A16: {[2] [1] [-3]} A17: {[2] [1] [-3]} A18: {[2] [1] [-3]} A19: {[2] [1] [-3]} A20: {[2] [1] [-3]} A21: {[2] [1] [-3]} A22: {[2] [1] [-3]} A23: {[2] [1] [-3]} A24: {[2] [1] [-3]} A25: {[2] [1] [-3]} A26: {[2] [1] [-3]} A27: {[2] [1] [-3]} A28: {[2] [1] [-3]} A29: {[2] [1] [-3]} A30: {[2] [1] [-3]}
In your code, in the line “A=’A’+num2str(i)” you have used single inverted commas on "A" which causes the addition to give the result as sum of the ASCII values of "A" and the ASCII value of the number stored in "i". Hence "A" becomes the sum of the ASCII values (for i=1 it becomes 114) which is a numeric value. This is causing the error as a numeric value cannot be used as field name. You can refer to the following documentation for more information:
##### 1 个评论显示 -1更早的评论隐藏 -1更早的评论
Dyuman Joshi 2024-3-27
(As Stephen has alread mentioned above) This is a notoriously bad coding practice - Forcing psuedo indices into fieldnames.
Instead one should use indices to store and access data as Rik and I have shown.
Why doing this is an awful/substandard coding method is explained in length, breath and width here (by none other than Stephen only) - https://in.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

### 类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

### Community Treasure Hunt

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

Start Hunting!

Translated by