필터 지우기
필터 지우기

How can i read different data types (strings numbers vectors) from one .txt-File?

조회 수: 4 (최근 30일)
At the moment i am using a .txt - File for my data input. (Only numbers with 3 columns)
In this .txt-file I want also store all the relevant formating data for my diagram. -> see attachment file. So i can use this parameters in my matlab code. How can i read them?
This was my old matlab code just with my Data Input:
Input_Matrix = textread('InputXX.txt')
[zeilen,spalten]=size(Input_Matrix)
x = Input_Matrix(:,1)
y = Input_Matrix(:,2)
z = Input_Matrix(:,3)
Thank you guys. Sincelery Philipp
  댓글 수: 4
Guillaume
Guillaume 2016년 11월 8일
There are some existing formats such as xml or json that are text (thus human readable / writable) and that matlab can easily parse if you want to be able to write your input data with a text editor. Or you could write a script that generates matlab variables.
Otherwise, the best way to store this sort of things is in a mat file, but of course it's not something that you can write / edit in a text editor.
Note that parsing your file is not that complicated (depending how fault tolerant you want your parser to be).

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

채택된 답변

Guillaume
Guillaume 2016년 11월 8일
As per my comment you could use an xml file and xmlread or a json file and jsondecode (the latter being a lot less verbose) but it means a substantial change of syntax for your input file.
It's not that complicated to write a parser for your current format. This would work (although lacking a lot of syntax validation):
diagramoptions = [];
diagramlimits = [];
inputdata = [];
wholecontent = fileread('InputXX.txt');
sections = regexp(wholecontent, '\**([^*]*)\**([^*]*)', 'tokens');
for section = sections
switch(strtrim(section{1}{1}))
case 'Diagram Options'
keyvalues = regexp(section{1}{2}, '([^\n\r=]*)=([^\n\r=]*)', 'tokens');
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));
otherwise
warning('Unknown section: %s', section{1}{1});
end
end
%see the outputs
openvar diagramoptions
openvar diagramlimits
openvar inputdata
  댓글 수: 5
Philipp Mueller
Philipp Mueller 2016년 11월 16일
편집: Philipp Mueller 2016년 11월 16일
@Guillaume ok, i understood thank you :) Last question: For example i have 3 diagrams to plot. That means my inputXX.txt has 9 sections -> 3 times diagram options 3 times input data and 3 times diagram limits. how can i seperate it? Is it possible to use
  • openvar diagramoptions[i]
  • openvar diagramlimits[i]
  • openvar inputdata[i]
i...index -> 1,2 or 3 diagram. Thank you in advance
Guillaume
Guillaume 2016년 11월 16일
The simplest way would be to append to diagramoptions, diagramlimits, inputdata every time you encounter a new one. It's easily done by replacing the assignment with
diagramoptions{end+1} = cell2table(... %same for the other two
You can then index each section using cell array indexing:
openvar diagramoptions{1}

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by