use struct in loop for
11 views (last 30 days)
Show older comments
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.
4 Comments
Dyuman Joshi
on 27 Mar 2024
Edited: Dyuman Joshi
on 27 Mar 2024
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
s(2).A
Answers (2)
Rik
on 27 Mar 2024
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 Comments
Rik
on 27 Mar 2024
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
on 27 Mar 2024
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
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 Comment
Dyuman Joshi
on 27 Mar 2024
(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
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!