필터 지우기
필터 지우기

How can i read the value (string or number) of a variable?

조회 수: 4 (최근 30일)
Philipp Mueller
Philipp Mueller 2016년 12월 1일
댓글: Philipp Mueller 2016년 12월 1일
Hello,
I read my input-data from a .txt-File. Now i want to use some of this variables in my script for formating and calculations.... Maybe the question is strange but anyway.... How can i read the value of the variable number_of_plots or color? Is there an easy way except using a for loop + Delimiter and so on? Thank you
My Code:
diagramoptions = [];
wholecontent = fileread('aaa.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'})
otherwise
warning('Unknown section: %s', section{1}{1})
end
end
openvar diagramoptions

답변 (1개)

Jan
Jan 2016년 12월 1일
편집: Jan 2016년 12월 1일
What do you exactly want? Searching for a specific token or importing all tokens before the '='?
Searching for a specific token:
contentS = fileread('aaa.txt');
content = regexp(contentS, '\n', 'split')
pattern = 'number_of_plots';
match = strncmp(content, [pattern, '='], length(pattern) + 1);
Data.(pattern) = sscanf(content{match}, [pattern, '=%g'], 1);
  댓글 수: 3
Jan
Jan 2016년 12월 1일
@Philipp: What does "select directly from diagramoptions variable" exactly mean? Not directly out of the txt file? I don't get it. Please post again, what you want to achieve.
Please do not cross-post a question in different forums. Thanks.
Philipp Mueller
Philipp Mueller 2016년 12월 1일
Ok, I will post it again as a new question. Thank you in advance

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

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by