MATLAB Answers

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

1 view (last 30 days)
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

Accepted Answer

Peter Perkins
Peter Perkins on 2 Dec 2016
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 Comments
Peter Perkins
Peter Perkins on 11 Dec 2016
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!