Loading workspace variable contents into an array or for loop
I am new to Matlab but am well aware of the bad practice notion associated with dynamically creating workspace variables. Unfortunately, Matlab’s volumeSegmenter only allows saving of segmentations as either MAT-files or workspace variables, and the former creates far too many individual files for the amount I require.
In the next step after creating them, I need to run all the segmentations (workspace vars seg1, seg2, seg3 …) through a for loop. I am currently using who() to try and find all the needed workspace variables, but this doesn’t work as only the names are stored in cells seg_options and cannot be called as variables:
vars = who();
find = contains(vars, ‘seg’);
seg_options = vars(find);
This is part of the for loop I need to call the segmentation variables for:
for i = 1:length(seg_options);
A = double(seg_options(i));
end
which obviously doesn’t work properly as I need to be calling the actual variable and not just its name.
The code also needs to work for a flexible number of segmentations (ie I cannot initialize an array as a specific size). Is there a way to:
1) load the workspace variable into array, overwrite it, load the next one into the next array cell, etc. (ie saves the first segmentation as seg, loads into seg_array cell 1, saves the next segmentation as seg, load that into seg_array cell 2, and so on)
2) load all the created variables (seg1, seg2…) into an array seg_array
or
3) call and loop through all the workspace variables in the for loop itself – I know this is not ideal
Thanks in advance!I am new to Matlab but am well aware of the bad practice notion associated with dynamically creating workspace variables. Unfortunately, Matlab’s volumeSegmenter only allows saving of segmentations as either MAT-files or workspace variables, and the former creates far too many individual files for the amount I require.
In the next step after creating them, I need to run all the segmentations (workspace vars seg1, seg2, seg3 …) through a for loop. I am currently using who() to try and find all the needed workspace variables, but this doesn’t work as only the names are stored in cells seg_options and cannot be called as variables:
vars = who();
find = contains(vars, ‘seg’);
seg_options = vars(find);
This is part of the for loop I need to call the segmentation variables for:
for i = 1:length(seg_options);
A = double(seg_options(i));
end
which obviously doesn’t work properly as I need to be calling the actual variable and not just its name.
The code also needs to work for a flexible number of segmentations (ie I cannot initialize an array as a specific size). Is there a way to:
1) load the workspace variable into array, overwrite it, load the next one into the next array cell, etc. (ie saves the first segmentation as seg, loads into seg_array cell 1, saves the next segmentation as seg, load that into seg_array cell 2, and so on)
2) load all the created variables (seg1, seg2…) into an array seg_array
or
3) call and loop through all the workspace variables in the for loop itself – I know this is not ideal
Thanks in advance! I am new to Matlab but am well aware of the bad practice notion associated with dynamically creating workspace variables. Unfortunately, Matlab’s volumeSegmenter only allows saving of segmentations as either MAT-files or workspace variables, and the former creates far too many individual files for the amount I require.
In the next step after creating them, I need to run all the segmentations (workspace vars seg1, seg2, seg3 …) through a for loop. I am currently using who() to try and find all the needed workspace variables, but this doesn’t work as only the names are stored in cells seg_options and cannot be called as variables:
vars = who();
find = contains(vars, ‘seg’);
seg_options = vars(find);
This is part of the for loop I need to call the segmentation variables for:
for i = 1:length(seg_options);
A = double(seg_options(i));
end
which obviously doesn’t work properly as I need to be calling the actual variable and not just its name.
The code also needs to work for a flexible number of segmentations (ie I cannot initialize an array as a specific size). Is there a way to:
1) load the workspace variable into array, overwrite it, load the next one into the next array cell, etc. (ie saves the first segmentation as seg, loads into seg_array cell 1, saves the next segmentation as seg, load that into seg_array cell 2, and so on)
2) load all the created variables (seg1, seg2…) into an array seg_array
or
3) call and loop through all the workspace variables in the for loop itself – I know this is not ideal
Thanks in advance! array, cell array, variable, variables, arrays, cell arrays, image segmentation, for loop MATLAB Answers — New Questions