I want to get the value of a variable from a table

조회 수: 1 (최근 30일)
Philipp Mueller
Philipp Mueller 2016년 12월 1일
댓글: Peter Perkins 2016년 12월 11일
As you can see my input is a .txt-File. This .txt-File has several sections for plotting diagrams. One section is my diagram options. I need this variables for formatting my scatter diagram. You can see i store all variables from the section Diagram Options to the variable (table) diagramoptions.
So now to my question: I want to get the value from the variable number_of_plots from the diagramoptions table. How can i do this? Here is my Code and Input-File.
thank you
diagramoptions = [];
diagramlimits = [];
inputdata = [];
diagramoptions2 = [];
diagramlimits2 = [];
inputdata2 = [];
diagramoptions3 = [];
diagramlimits3 = [];
inputdata3 = [];
wholecontent = fileread('Rainflow_Data_Limits_Settings - Copy.txt')
sections = regexp(wholecontent, '\*+([^*]+)\*+([^*]+)', 'tokens')
for section = sections
switch(strtrim(section{1}{1}))
% if strcmp(strtrim(section{1}{1}), 'Diagram Options')
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'})
% end
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

채택된 답변

Peter Perkins
Peter Perkins 2016년 12월 2일
Phillipp, I'm not exactly sure what you're aksing to do, but in general if you have a table named diagramoptions containing a variable named number_of_plots, then
diagramoptions.number_of_plots
returns the entire variable as (probably) a vector in whatever its "native" type is, and
diagramoptions.number_of_plots(5)
returns the 5th element of that variable. This is described in the doc .
  댓글 수: 2
Philipp Mueller
Philipp Mueller 2016년 12월 9일
thank you for your support. but it does not work so far. -> if i use your command i get this Error using table/dotParenReference (line 52) Unrecognized variable name 'number_of_plots'.
Peter Perkins
Peter Perkins 2016년 12월 11일
Then apparently your table doesn't have a variable with that name. The documentation is pretty thorough on table subscripting, that's the place to start.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by