Replace the variable with it's contents to access a struct

5 次查看(过去 30 天)
I have a list of 20+ struct that i want to do some math on. Since i need to to similar math to several of the structs i want to automate it as much as possible.
The structs are named Axxxx so i made a for loop where i create value in the variable that is the name of the struct. How do i then use whats in the variable with the struct.
First = 'A00';
for k = 1 : 21
A_temp = append(First,num2str(k-1));
A_temp.new_column = A_temp.old_column/15; <--- This does not work, but might display what i want to do.
end
A_temp now contains say 'A0020' as a string, how do i access that struct using the variable A_temp containing 'A0020'
A_temp.kanal1 becomes A0020.kanal1
I hope my question is understandable
Kind Regards
David
  3 个评论
Stephen23
Stephen23 2022-10-4
编辑:Stephen23 2022-10-4
"The structs are named Axxxx ..."
And that is the start of your problems, right there.
"Since i need to to similar math to several of the structs i want to automate it as much as possible."
And so you designed your data to make that automation easier, e.g. by creating one cell/structure array which can be trivially accessed using basic, neat, very efficient indexing. Or did you skip that important step?
"...so i made a for loop where i create value in the variable that is the name of the struct. How do i then use whats in the variable with the struct."
Aaaahh, unfortunately that bad data design forces you into writing slow, complex, inefficient, insecure, obfuscated, buggy code which is difficult to debug:
Accessing your data would be trivial and very efficient with better data design e.g. using one structure array would let you use indexing. So far you have not told us the most important information: how did you get those tables into the workspace? The place where they are created in the workspace is the correct place to fix your code, e.g. by LOADing into an output variable instead of LOADing directly into the workspace.
David Oestlund
David Oestlund 2022-10-4
Ok, i think i understand.
The data comes from an assignment. There is a file when runned it will create 25 structs from some files.
So it is what it is and i will do the assingment instead of putting time in to this. Since the problem is in the creators end i will take that as it was intended.
Thank you very much for your time

请先登录,再进行评论。

回答(1 个)

Vishesh
Vishesh 2022-10-7
  • You can do it by saving all the variables present in the workspace in a table first and then perform your operation in the table.
  • Here i am taking "A001" and "A002" as an example.
  • You can optimize some of the lines of code by yourself.
clear;
A001=struct('a',2,'b',10);
A002=struct('a','4','b',20);
save('var.mat');
clear;
Tab=struct2table(load('var.mat'),'AsArray',true);
First = 'A00';
var_name=Tab.Properties.VariableNames;
var_names=[];
for i=1:length(var_name)
var_names=[var_names ;var_name{i}];
end
for k = 1 : 21
A_temp = append(First,num2str(k-1));
for i=1:length(var_names(:,1))
if(strcmp(var_names(i,:),A_temp))
Tab.(i).a=Tab.(i).a/2;
Tab.(i).b=Tab.(i).b/5;
end
end
end
clear A_temp First i k var_names var_name
table2struct(Tab)
ans = struct with fields:
A001: [1×1 struct] A002: [1×1 struct]

类别

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