How to rename a structure array with a string stored in character

I have a bunch of structure arrays in workspace that need to be manipulated.
For sake of simplicity, let's consider only 3 structure arrays named as "Signal_1", "Signal_2", "Signal_3"
Each structure had a set of time data, which I am truncating based on certain conditions being met. Say I have data from 0-10 seconds, and I am truncating the arrays to keep only between t=3 and t=5 seconds. I am able to do this by first making a copy of each structure array into a temporary variable called "temp".
Now I want to export the truncated data in the same structure arrays, so that I can use it in another tool.
Is there a way to
  1. Rename the "temp" structure array as "Signal_1". Original structure "Signal_1" exists in the workspace, so it needs to be deleted first, and the name of Signal_1 is stored in cell array. I can clear "Signal_1", how do I rename "temp". In the end I need to export a mat file which has all "Signal_X" variables,
  2. Directly access the original structure arrays without having to copy it in a temporary varaible? I believe it is not recommeded to dynmic assignments so I have not explored this. All I have read is "Don't do it" but wanted to ask because I cannot think of any other way to manipulate the data as I have explained above.

1 Comment

So far you have not told us the most important information: how exactly did you get all of those variables into the workspace?
The points where they are created in the workspace is the place to fix your code. For example, by LOADing into an output variable (which you should always be doing anyway).

Sign in to comment.

Answers (1)

Just make copies of the originals if you need to keep them
Signal_1_original = Signal_1;
Signal_2_original = Signal_2;
Signal_3_original = Signal_3;
% Crop time data for first structure array
% Find out how many structures are in the structure array
numStructures1 = numel(Signal_1)
% Crop the time array in every structure.
for k = 1 : numStructures1
% Get a mask for the k'th structure in the Signal_1 array.
mask = Signal_1(k).timeData >= 3 & Signal_1(k).timeData <= 5;
% Extract the time data within the time mask for the k'th structure in the Signal_1 array.
Signal_1(k).timeData = Signal_1(k).timeData(mask);
end
% Now repeat for the other two structure arrays.
If you don't know how many structure arrays you will have, then somehow you're reading them in and I'd suggest you don't give them different names. Just process them in a loop as they come in (preferred), or else load them all into individual cells of a cell array if (for some reason) you need them all in memory at the same time (less preferred).

Categories

Asked:

on 5 Dec 2023

Commented:

on 5 Dec 2023

Community Treasure Hunt

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

Start Hunting!