0

I have a matlab workspace that only contains structures. All structures have the same fields. Imagine I have three structures in the workspace with names:

B00002N6AA  B00002N6VF  B00004OKOP  

I can combine them into a single structure by doing:

combined = [B00002N6AA  B00002N6VF  B00004OKOP];

Now I have thousands of structures. I know I can get all their names by doing:

SNames = who;

Is there anyway of combining them all into a single structure without having to manually copy and paste their names?

Suever
  • 64,497
  • 14
  • 82
  • 101
phdstudent
  • 1,060
  • 20
  • 41

1 Answers1

3

You can save all of your data to a file and then reload this file into a struct and then use struct2array to convert it into an array of structs.

filename = [tempname, '.mat'];

% Save all variables starting with B0000
save(filename, 'B0000*')

% load the data back into a struct
tmp = load(filename, '-mat');

% Convert this struct into an array of structs
result = struct2array(tmp);

% Delete the temporary file
delete(filename)

In the future, it's best to avoid using dynamic variable names which encode data. Instead, store the data inside the data structure itself instead.

Suever
  • 64,497
  • 14
  • 82
  • 101