create a struct with two columns

조회 수: 20 (최근 30일)
Alberto Acri
Alberto Acri 2023년 6월 14일
댓글: Stephen23 2023년 6월 14일
Hi. I want to create a structure like the one in the figure consisting of two columns: 'name' and 'folder'.
I already have the char (created by a for loop) of:
for k = 1:7
name_char % = '0001';
folder_char %= 'C\.....';
end
I tried to create the structure but I don't know how to insert all the values inside name_char and folder_char generated by the for loop.
first_column = 'name_char';
second_column = 'folder_char';
s = struct(first_column,name_char,second_column,folder_char);
How can I do it?
  댓글 수: 1
Stephen23
Stephen23 2023년 6월 14일
"Hi. I want to create a structure like the one in the figure consisting of two columns: 'name' and 'folder'."
Actually your structure has exactly one column, and six fields. This is clearly shown in the screenshot.
Do not confuse the number of fields with the size of a structure, they are completely unrelated.

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

채택된 답변

Stephen23
Stephen23 2023년 6월 14일
"How can I do it?"
S = struct([]);
for k = 1:7
name_char % = '0001';
folder_char %= 'C\.....';
S(k).name = name_char;
S(k).folder = folder_char;
end

추가 답변 (1개)

Satwik
Satwik 2023년 6월 14일
for k = 1:7
name_char = '0001';
folder_char = 'C\.....';
end
first_column = 'name_char';
second_column = 'folder_char';
s = struct('first_column',{name_char},'second_column',{folder_char});
s = struct with fields:
first_column: '0001' second_column: 'C\.....'
  댓글 수: 1
Alberto Acri
Alberto Acri 2023년 6월 14일
Thank you for your reply.
Perhaps my question is misleading.
I would like, at the end of each for loop, the values of name_char and folder_char to be inserted inside the structure consisting of two columns.

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

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by