How to loop over different named structs?

Hi All,
I have a set of cursor_info's which containt the x,y- coordinates generated from different figures, as such:
cursor_info 1x1 struct
cursor_info1 1x1 struct
cursor_info2 1x1 struct
cursor_info3 1x1 struct
.
.
cursor_infoN 1x1 struct
By using a for loop and the cell2mat funtction i'm trying to get all cursor info in matrix form, like this
for i = 1:N
All_data(i,:) = cell2mat({cursor_info{I}.Position}');
end
But I don't know how to change the cursor_info's name in the for loop from cursor_info to cursor_info1 and so on.
Thanks in advace!

2 件のコメント

Stephen23
Stephen23 2021 年 5 月 20 日
@Aris van Houten: you forgot to tell us the most important piece of information: how did you get all of those individual variables into the workspace? Did you name them all by hand? Did you load them from a mat file (or files)? Were they created by a badly-written script?
Aris van Houten
Aris van Houten 2021 年 5 月 20 日
They are all individually created by the data cursor tool in the figure window. Followed by exporting data cursor to workspace option.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2021 年 5 月 20 日
編集済み: Stephen23 2021 年 5 月 20 日

1 投票

cursor_info.Position = [0,1,2,3];
cursor_info1.Position = [1,1,2,3];
cursor_info2.Position = [2,1,2,3];
cursor_info10.Position = [10,1,2,3];
cursor_info.Whatever = 0;
cursor_info1.Whatever = 1;
cursor_info2.Whatever = 2;
cursor_info10.Whatever = 10;
%
save('temp.mat','-regexp','cursor_info\d*');
S = load('temp.mat')
S = struct with fields:
cursor_info: [1×1 struct] cursor_info1: [1×1 struct] cursor_info10: [1×1 struct] cursor_info2: [1×1 struct]
F = fieldnames(S);
V = str2double(regexp(F,'\d*$','match','once'));
V(isnan(V)) = 0;
[~,X] = sort(V);
C = struct2cell(S);
S = [C{X}]
S = 1×4 struct array with fields:
Position Whatever
P = vertcat(S.Position)
P = 4×4
0 1 2 3 1 1 2 3 2 1 2 3 10 1 2 3

1 件のコメント

Aris van Houten
Aris van Houten 2021 年 5 月 20 日
Thanks a million times, Stephen!

サインインしてコメントする。

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 5 月 20 日

0 投票

for ii = 1:N
All_data(ii,:) = cell2mat({['cursor_info' num2str(ii)].Position}');
end
Good luck

1 件のコメント

Aris van Houten
Aris van Houten 2021 年 5 月 20 日
It does not recognize the .Position

サインインしてコメントする。

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by