How programmatically create an array from variable-length vectors?
Hello
I have ~500 .mat files, generated by a separate software, which all contain a structure called s1.a. This structure has variable length for each file, and I don’t know this length until each file is loaded into the workspace. I cannot control the naming of the files nor the naming of the structures ahead of time. Example:
s1.a = [1 3 4 2];
s1.a = [4 6 2 9 0 4];
s1.a = [0 9 5 7 2];
s1.a = [4 8 6 1];
s1.a = [5 7 2 9 4 0 1];
…
s500.a = [3 2 0];
I need to end up with a single structure at the end which concatenates all of the individual s1.a structures:
sFinal.a = [1 3 4 2 4 6 2 9 0 4 0 9 5 7 2 4 8 6 1 5 7 2 9 4 0 1 … 3 2 0];
If all structures had the same length, I can easily write a for loop to load each file and then build the array. But I’m struggling with the variable length aspect of each structure.
One thought I had was to pre-allocate an array with NaNs that is longer than the max length of the s1.a structure (which is 3600), thus "forcing" them to be of the same length by populating the first numel(s1.a) slots, and then once the final structure is built, simply removing all the NaNs. But this seems rather convoluted and not efficient.
Any suggestions?
Thank you!Hello
I have ~500 .mat files, generated by a separate software, which all contain a structure called s1.a. This structure has variable length for each file, and I don’t know this length until each file is loaded into the workspace. I cannot control the naming of the files nor the naming of the structures ahead of time. Example:
s1.a = [1 3 4 2];
s1.a = [4 6 2 9 0 4];
s1.a = [0 9 5 7 2];
s1.a = [4 8 6 1];
s1.a = [5 7 2 9 4 0 1];
…
s500.a = [3 2 0];
I need to end up with a single structure at the end which concatenates all of the individual s1.a structures:
sFinal.a = [1 3 4 2 4 6 2 9 0 4 0 9 5 7 2 4 8 6 1 5 7 2 9 4 0 1 … 3 2 0];
If all structures had the same length, I can easily write a for loop to load each file and then build the array. But I’m struggling with the variable length aspect of each structure.
One thought I had was to pre-allocate an array with NaNs that is longer than the max length of the s1.a structure (which is 3600), thus "forcing" them to be of the same length by populating the first numel(s1.a) slots, and then once the final structure is built, simply removing all the NaNs. But this seems rather convoluted and not efficient.
Any suggestions?
Thank you! Hello
I have ~500 .mat files, generated by a separate software, which all contain a structure called s1.a. This structure has variable length for each file, and I don’t know this length until each file is loaded into the workspace. I cannot control the naming of the files nor the naming of the structures ahead of time. Example:
s1.a = [1 3 4 2];
s1.a = [4 6 2 9 0 4];
s1.a = [0 9 5 7 2];
s1.a = [4 8 6 1];
s1.a = [5 7 2 9 4 0 1];
…
s500.a = [3 2 0];
I need to end up with a single structure at the end which concatenates all of the individual s1.a structures:
sFinal.a = [1 3 4 2 4 6 2 9 0 4 0 9 5 7 2 4 8 6 1 5 7 2 9 4 0 1 … 3 2 0];
If all structures had the same length, I can easily write a for loop to load each file and then build the array. But I’m struggling with the variable length aspect of each structure.
One thought I had was to pre-allocate an array with NaNs that is longer than the max length of the s1.a structure (which is 3600), thus "forcing" them to be of the same length by populating the first numel(s1.a) slots, and then once the final structure is built, simply removing all the NaNs. But this seems rather convoluted and not efficient.
Any suggestions?
Thank you! concatenate, for loop, variable length MATLAB Answers — New Questions