Variable Cell Array to Dynamic Structure

2 views (last 30 days)
Hello!
I am working on a project that opens files into a dynamic structure using the uigetfile().
The code uses boolean statements to perform calculations on the data from several different file types using variable naming convetions 'extens', 'frac', 'DAQ, 'MTS', as the suffixes associaed with each branch.
The code then creates a cell array of the variables using whos, uses a for loop to cycle through the aray, convert each cell to a string, then builds a second dynamic structure for dataSummary, which will eventually be exported in an excel document.
Currently, the structure's value is associated to the string of text from the variable name cell array. I need a way to replace the val_i string with the actual values associated with the variable the string matches.
Any help or pointers would be greatly apreciated!
--PS I know that dynamic variable creation is a big no no , that is why I used the dynamic structure/fieldnames creation as recmended here
my code:
% will load over 50 variables that need to be sorted
all_current_variables = who;
for i=1:length(all_current_variables)
%converts to string
val_i=all_current_variables{i,1};
% searches string for the section of the new structure
% the variable belongs in
if strfind(val_i,'extens')==1
%sorts into branch dictated by the suffix then uses the val_i to creat
%new field,
DataSummary.DIC_Extens.(val_i)=...
val_i;
% issue is here I want the field to be associated with the
% values of the varrable
% currently is only associating with the string that
% matchesthe variables name in the workspace
elseif strfind(val_i,'frac')==1
DataSummary.DIC_Fracbox.(val_i)=val_i;
elseif strfind(val_i,'DAQ')==1
DataSummary.DAQ.(val_i)=val_i;
elseif strfind(val_i,'MTS')==1
DataSummary.MTS.(val_i)=val_i;
end
end
  2 Comments
Marguerite Kennish
Marguerite Kennish on 2 Jul 2020
How do I access the arrays associated with the variables in the workspace, within the loop so that the field names match the varaiables.
Right now this section of code creates the organized structure I need based on the variables that are created earlier in the code,
but the field only ends up equating to the string from all_current_variables (ex: ='force_MTS' )
not to the variable in the workspace that matches the string (ex: =force_MTS (which is a 5799x1 double) ).

Sign in to comment.

Accepted Answer

Marguerite Kennish
Marguerite Kennish on 2 Jul 2020
I made little mistake the strifind to contains, but the eval() fixed my issues quickly thank you for your time.
%willload over 50 variables that need to be sorted
all_current_variables = who;
%converts to string
for i=1:length(all_current_variables)
val_i=all_current_variables{i,1};
% searches string for the section of the new structure
% the variable belongs in
if contains(val_i,'extens')==1
%sorts into branch dictated by the suffix then uses the val_i to creat
%new field,
DataSummary.DIC_Extens.(val_i)=...
eval(val_i);
elseif contains(val_i,'frac')==1
DataSummary.DIC_Fracbox.(val_i)=eval(val_i);
elseif contains(val_i,'DAQ')==1
DataSummary.DAQ.(val_i)=eval(val_i);
elseif contains(val_i,'MTS')==1
DataSummary.MTS.(val_i)=eval(val_i);
end
end

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!