필터 지우기
필터 지우기

Variable Cell Array to Dynamic Structure

조회 수: 4 (최근 30일)
Marguerite Kennish
Marguerite Kennish 2020년 7월 2일
답변: Marguerite Kennish 2020년 7월 2일
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
madhan ravi
madhan ravi 2020년 7월 2일
What is your question exactly?
Marguerite Kennish
Marguerite Kennish 2020년 7월 2일
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) ).

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

채택된 답변

Marguerite Kennish
Marguerite Kennish 2020년 7월 2일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by