Defining a hardcoded matlab code dynamically? How can i do this in order of my code?

4 Ansichten (letzte 30 Tage)
Is there the possibility to create my diagram options(i), diagramlimits(i) and inputdata(i) dynamically with a for loop - or with another way -> i=3? I don't want to write it hard coded like in my code at the moment.
At the moment a big part of my code is repeating. Only the variable names are changing. I will avoid this. I will replace it with variables based with an index??? My Code:
clear all %löscht alle Variablen
clear clc %löscht den Bildschirm
diagramoptions = [];
diagramlimits = [];
inputdata = [];
diagramoptions2 = [];
diagramlimits2 = [];
inputdata2 = [];
diagramoptions3 = [];
diagramlimits3 = [];
inputdata3 = [];
wholecontent = fileread('Rainflow_Data_Limits_Settingsx.txt')
sections = regexp(wholecontent, '\*+([^*]+)\*+([^*]+)', 'tokens')
for section = sections
switch(strtrim(section{1}{1}))
case 'Diagram Options' %Diagram Options -> siehe meine Gliederung im .txt file
keyvalues = regexp(section{1}{2}, '([^\n\r=]+)=([^\n\r=]+)', 'tokens')%\n -> new line; \r carriage return
diagramoptions = cell2table(vertcat(keyvalues{:}), 'VariableNames', {'Key', 'Value'})
case 'Diagram Limits'
header = strsplit(regexp(section{1}{2}, '[^\n\r]*', 'match', 'once'))
content = textscan(section{1}{2}, repmat('%f', 1, numel(header)), 'HeaderLines', 2)
diagramlimits = table(content{:}, 'VariableNames', header)
case 'Input Data'
inputdata = cell2mat(textscan(section{1}{2}, '%f%f%f', 'HeaderLines', 1))%dh: ich habe 1 Headerline zur besseren übersicht
case 'Diagram Options2' %Diagram Options -> siehe meine Gliederung im .txt file
keyvalues2 = regexp(section{1}{2}, '([^\n\r=]+)=([^\n\r=]+)', 'tokens')%\n -> new line; \r carriage return
diagramoptions2 = cell2table(vertcat(keyvalues{:}), 'VariableNames', {'Key', 'Value'})
case 'Diagram Limits2'
header2 = strsplit(regexp(section{1}{2}, '[^\n\r]*', 'match', 'once'))
content2 = textscan(section{1}{2}, repmat('%f', 1, numel(header2)), 'HeaderLines', 2)
diagramlimits2 = table(content2{:}, 'VariableNames', header2)
case 'Input Data2'
inputdata2 = cell2mat(textscan(section{1}{2}, '%f%f%f', 'HeaderLines', 1))%dh: ich habe 1 Headerline zur besseren übersicht
case 'Diagram Options3' %Diagram Options -> siehe meine Gliederung im .txt file
keyvalues3 = regexp(section{1}{2}, '([^\n\r=]+)=([^\n\r=]+)', 'tokens')%\n -> new line; \r carriage return
diagramoptions3 = cell2table(vertcat(keyvalues{:}), 'VariableNames', {'Key', 'Value'})
case 'Diagram Limits3'
header3 = strsplit(regexp(section{1}{2}, '[^\n\r]*', 'match', 'once'))
content3 = textscan(section{1}{2}, repmat('%f', 1, numel(header3)), 'HeaderLines', 2)
diagramlimits3 = table(content3{:}, 'VariableNames', header3)
case 'Input Data3'
inputdata3 = cell2mat(textscan(section{1}{2}, '%f%f%f', 'HeaderLines', 1))%dh: ich habe 1 Headerline zur besseren übersicht
otherwise
warning('Unknown section: %s', section{1}{1})
end
end
%öffnet die output fenster
openvar diagramoptions
openvar diagramlimits
openvar inputdata
openvar diagramoptions2
openvar diagramlimits2
openvar inputdata2
openvar diagramoptions3
openvar diagramlimits3
openvar inputdata3
For your interest -> things i have tried With no success. For example:
for i = 1:count
str = num2str(i);
eval(['diagramoptions' num2str(i) '=dataset;']);
eval(['diagramlimits' num2str(i) '=dataset;']);
eval(['inputdata' num2str(i) '=dataset;']);
eval(['header' num2str(i) '=dataset;']);
eval(['keyvalues' num2str(i) '=dataset;']);
eval(['content' num2str(i) '=dataset;']);
end
zzz=3
for k = 1:1:3
assignin ('base',['variable_' num2str(k)], zzz);
end
%%%%%%%Then i tried to replace the old variable name with the new one with an index -> error
Thank you in advance

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by