Loading workspace variable contents into an array or for loop

조회 수: 15 (최근 30일)
stephen
stephen 2024년 8월 8일
편집: stephen 2024년 8월 14일
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!
  댓글 수: 2
Stephen23
Stephen23 2024년 8월 9일
편집: Stephen23 2024년 8월 9일
What is the approximate memory required for all of those variables? (e.g. use WHOS then sum their memory consumption).
stephen
stephen 2024년 8월 14일
편집: stephen 2024년 8월 14일
Not excessive, it's more of a convenience thing for me. I'd have to save all of those segmentation vars per run of this code, and do that over a dozen times on various sets of files. I'd just rather have them tidily in an array instead of lying around as seg1 seg 2... up to however many dozens, which makes saving the workspace itself pretty slow. If you can suggest an even better method than the answer given that'd be awesome!

댓글을 달려면 로그인하십시오.

채택된 답변

Aditya
Aditya 2024년 8월 8일
You can load workspace variables into an array and process them in a loop using the following approach:
1) Identify Segmentation Variables
  • You have already done this part using the "who" and "contains" functions.
2) Load Variables into an Array
  • You can use the "eval" function to dynamically load the variables into an array.
3) Loop Through the Array to Process Each Segmentation
  • Once the variables are loaded into an array, you can process them in a loop.
Here’s the complete code:
% Identify segmentation variables
vars = who();
find = contains(vars, 'seg');
seg_options = vars(find);
% Load variables into a cell array
seg_array = cell(1, length(seg_options));
for i = 1:length(seg_options)
seg_array{i} = eval(seg_options{i});
end
% Process each segmentation
for i = 1:length(seg_array)
A = double(seg_array{i});
% Your processing code here
end
Hope this helps!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by