从 MATLAB 获取数据的问题

发布于 2024-09-17 07:01:52 字数 366 浏览 4 评论 0原文

之前请求帮助从生成的文本文件中读取数据一个 C 程序(一个 Exe)。

使用 @second 的解决方案,我解决了问题,但是昨天我发现输出文件比我预期的更复杂。

文件输出为:

V|0|0|0|t|0|1|1|4|11|T4|H13

I previously asked for help in reading data from a text file generated by a C program (an Exe).

Using @second's solution, I solved the problem but yesterday I discovered that the output file is more complex than I had expected.

The file output is:

V|0|0|0|t|0|1|1|4|11|T4|H13

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

听你说爱我 2024-09-24 07:01:52

||
P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0
*|A1|A1|A7|A16|F|F|F|F|F|F

||
P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0
*|A1|A1|A7|A16|F|F|F|F|F|F

缱倦旧时光 2024-09-24 07:01:52

|||
*|codserv|area|codice|nome|tnom|tmin|tmax|pc|qc|susc|||||||
*|||||kV|kV|kV|MW|MVAR|S

|||
*|codserv|area|codice|nome|tnom|tmin|tmax|pc|qc|susc|||||||
*|||||kV|kV|kV|MW|MVAR|S

英雄似剑 2024-09-24 07:01:52

||
N|I|1|N01|N01|132|125.4|138.6|0|0||||||||
N|I|1|N02|N02|20|19|21|0|0

||
N|I|1|N01|N01|132|125.4|138.6|0|0||||||||
N|I|1|N02|N02|20|19|21|0|0

枯叶蝶 2024-09-24 07:01:52

|||
N|I|1|N03|N03|20|19|21|1.013532234|0.49087611||||||||
N|I|1|N04|N04|20|19|21|0.390791617|0.189269056||||||||
N|I|1|N05|N05|20|19|21|0.180634542|0.121387171||||||||
N|I|1|N06|N06|20|19|21|0.709472564|0.343613323||||||||
N|I|1|N07|N07|20|19|21|0.103495727|0.069549543||||||||
N|I|1|N08|N08|20|19|21|0.351712456|0.170342158||||||||
N|I|1|N09|N09|20|19|21|0.097697904|0.06565339||||||||
N|I|1|N10|N10|20|19|21|0.162165157|0.078540184||||||||
N|I|1|N11|N11|20|19|21|0|0||||||||
*|A1|A8|A7|A7|F|F|F|F||F||F
*|plev|area|codice|estr1|estr2|lung|imax|rsd|xsd|bsd1|bsd2|||
*|||||km|A|Ohm|Ohm||S||S
L|I|D10203|N02|N03|1.884|360|0.41071|0.207886957|3.19E-08|3.19E-08

|||
N|I|1|N03|N03|20|19|21|1.013532234|0.49087611||||||||
N|I|1|N04|N04|20|19|21|0.390791617|0.189269056||||||||
N|I|1|N05|N05|20|19|21|0.180634542|0.121387171||||||||
N|I|1|N06|N06|20|19|21|0.709472564|0.343613323||||||||
N|I|1|N07|N07|20|19|21|0.103495727|0.069549543||||||||
N|I|1|N08|N08|20|19|21|0.351712456|0.170342158||||||||
N|I|1|N09|N09|20|19|21|0.097697904|0.06565339||||||||
N|I|1|N10|N10|20|19|21|0.162165157|0.078540184||||||||
N|I|1|N11|N11|20|19|21|0|0||||||||
*|A1|A8|A7|A7|F|F|F|F||F||F
*|plev|area|codice|estr1|estr2|lung|imax|rsd|xsd|bsd1|bsd2|||
*|||||km|A|Ohm|Ohm||S||S
L|I|D10203|N02|N03|1.884|360|0.41071|0.207886957|3.19E-08|3.19E-08

余生再见 2024-09-24 07:01:52

|||
L|I|D10304|N03|N04|1.62|360|0.35316|0.1787563|3.19E-08|3.19E-08

|||
L|I|D10304|N03|N04|1.62|360|0.35316|0.1787563|3.19E-08|3.19E-08

倚栏听风 2024-09-24 07:01:52

|||
L|I|D10405|N04|N05|0.532|360|0.11598|0.058702686|3.19E-08|3.19E-08

|||
L|I|D10405|N04|N05|0.532|360|0.11598|0.058702686|3.19E-08|3.19E-08

信愁 2024-09-24 07:01:52

|||
L|I|D10506|N05|N06|1.284|360|0.27991|0.14168092|3.19E-08|3.19E-08

|||
L|I|D10506|N05|N06|1.284|360|0.27991|0.14168092|3.19E-08|3.19E-08

爱冒险 2024-09-24 07:01:52

|||
L|I|D10607|N06|N07|1.618|280|0.53879|0.194766124|3.00E-08|3.00E-08

|||
L|I|D10607|N06|N07|1.618|280|0.53879|0.194766124|3.00E-08|3.00E-08

月亮邮递员 2024-09-24 07:01:52

|||
L|I|D10708|N07|N08|0.532|280|0.17716|0.064039294|3.00E-08|3.00E-08

|||
L|I|D10708|N07|N08|0.532|280|0.17716|0.064039294|3.00E-08|3.00E-08

爺獨霸怡葒院 2024-09-24 07:01:52

|||
L|I|D10809|N08|N09|2|360|0.436|0.220686791|3.19E-08|3.19E-08

|||
L|I|D10809|N08|N09|2|360|0.436|0.220686791|3.19E-08|3.19E-08

妄司 2024-09-24 07:01:52

|||
L|I|D10910|N09|N10|2.4|360|0.5232|0.264824149|3.19E-08|3.19E-08

|||
L|I|D10910|N09|N10|2.4|360|0.5232|0.264824149|3.19E-08|3.19E-08

桜花祭 2024-09-24 07:01:52

||
*|A1|A8|A7|A7|F|F|A1|F|F|F|F|F|F||F||F

||
*|A1|A8|A7|A7|F|F|A1|F|F|F|F|F|F||F||F

白况 2024-09-24 07:01:52
别把无礼当个性 2024-09-24 07:01:52

|||||
*|codserv|codice|estr1|estr2|vn1|vn2|nod1|varp|varm|np|Pb|rsd|xsd||bsd1||bsd2

|||||
*|codserv|codice|estr1|estr2|vn1|vn2|nod1|varp|varm|np|Pb|rsd|xsd||bsd1||bsd2

悍妇囚夫 2024-09-24 07:01:52

||
*|||||kV|kV||%|%||MVA|%|%||%||%|||||
%%%%%------%%%%%------%%%% **(read up to here)**
other unnecessary data

该算法应该:

  • 跳过前 3 行
  • 跳过第五行

  • 对于第四行 *|codserv|area|codice|nome|tnom|tmin|tmax|pc| qc|susc|||||||,将每个字符串保存在空向量中 codeserv=[] area=[] codice=[] nome=[] tnom=[] tmin=[] tmax =[] pc=[] qc=[] susc=[]

  • 在第四个之后的行中用数据和字符串填充向量

    codeserv=[NNNNNNNNNN ....] 
    面积=[IIIIII....] 
    代码=[1 1 1 1 1 1 ...] 
    名称=[N01 N02 N03 N04 N05 ] 
    tnom=[N01 N02 N03 N04 N05] 
    tmin=[132 20 20.....] 
    tmax=[125.4 19 19 19 ....] 
    电脑=[138.6 21 21 21....] 
    qc=[0 0 1.013532234 ....] 
    susc=[0 0 0.49087611]
    
  • 对以字母 L 开头的数据执行相同的操作。读取此行 codice|estr1|estr2|lung|imax|rsd|xsd||bsd1||bsd2 并用以 L 开头的行中的值填充向量 p>

    plev=[LLLLLLL ....] 
    面积=[IIIIII....]
    代码=[D10203 D10304 ...] 
    estr1=[N02 N03 N04 N05 ...] 
    estr2=[N03 N04 N05...] 
    肺=[1.884 1,662 ....] 
    imax=[360 360 .....] 
    rsd=[数字....] 
    xsd=[数字....] 
    bsd1=[数字....] 
    bsd2=[数字....]
    

我尝试过适应上一个问题的代码,但考虑到以 NL 开头的行不知道我需要知道如何读取第一个字符串和数一下 NL 的数量。

read
[vp***NNNNNNNNNNNNNNNNNNNNNNNNLLLLLLLLLLLLLLLLLLLLL***] 

length N 

length L 

skip 1 2 3 line

read 4 line, create vector

codeserv=[N N N N N N N N N N ....] 
area=[I I I I I I I ....] 
codice=[1 1 1 1 1 1 ...] 
nome=[N01 N02 N03 N04 N05 ] 
tnom=[N01 N02 N03 N04 N05] 
tmin=[132 20 20.....] 
tmax=[125.4 19 19 19 ....] 
pc=[138.6 21 21 21....] 
qc=[0 0 1.013532234 ....] 
susc=[0 0 0.49087611]

skip length N +1 line

read *|plev|area|codice|estr1|estr2|lung|imax|rsd|xsd|bsd1|bsd2|||

skip length N +3 line

create

plev=[L L L L L L L ....] 
area=[I I I I I I I ....]
codice=[D10203 D10304 ...] 
estr1=[N02 N03 N04 N05  ...] 
estr2=[N03 N04 N05...] 
lung=[1.884 1,662 ....] 
imax=[360 360 .....] 
rsd=[number....] 
xsd=[number....] 
bsd1=[number ....] 
bsd2=[number....]

close the cycle 

我希望这是可以理解的。我最大的问题是计算文本中的 NL

||
*|||||kV|kV||%|%||MVA|%|%||%||%|||||
%%%%%------%%%%%------%%%% **(read up to here)**
other unnecessary data

The algorithm should:

  • skip the first 3 rows
  • skip fifth row

  • For the fourth row *|codserv|area|codice|nome|tnom|tmin|tmax|pc|qc|susc|||||||, save each string in a vector empty codeserv=[] area=[] codice=[] nome=[] tnom=[] tmin=[] tmax=[] pc=[] qc=[] susc=[]

  • Fill vectors with data and strings in the rows following the fourth

    codeserv=[N N N N N N N N N N ....] 
    area=[I I I I I I I ....] 
    codice=[1 1 1 1 1 1 ...] 
    nome=[N01 N02 N03 N04 N05 ] 
    tnom=[N01 N02 N03 N04 N05] 
    tmin=[132 20 20.....] 
    tmax=[125.4 19 19 19 ....] 
    pc=[138.6 21 21 21....] 
    qc=[0 0 1.013532234 ....] 
    susc=[0 0 0.49087611]
    
  • Do the same with the data starting with the letter L. Read this line codice|estr1|estr2|lung|imax|rsd|xsd||bsd1||bsd2 and fill the vectors with values in the lines beginning with L

    plev=[L L L L L L L ....] 
    area=[I I I I I I I ....]
    codice=[D10203 D10304 ...] 
    estr1=[N02 N03 N04 N05  ...] 
    estr2=[N03 N04 N05...] 
    lung=[1.884 1,662 ....] 
    imax=[360 360 .....] 
    rsd=[number....] 
    xsd=[number....] 
    bsd1=[number ....] 
    bsd2=[number....]
    

I tried to adapt the code from the previous question, but given that the lines that start with N and L do not know how many I need to know how to read the first string and count the number N and L are.

read
[vp***NNNNNNNNNNNNNNNNNNNNNNNNLLLLLLLLLLLLLLLLLLLLL***] 

length N 

length L 

skip 1 2 3 line

read 4 line, create vector

codeserv=[N N N N N N N N N N ....] 
area=[I I I I I I I ....] 
codice=[1 1 1 1 1 1 ...] 
nome=[N01 N02 N03 N04 N05 ] 
tnom=[N01 N02 N03 N04 N05] 
tmin=[132 20 20.....] 
tmax=[125.4 19 19 19 ....] 
pc=[138.6 21 21 21....] 
qc=[0 0 1.013532234 ....] 
susc=[0 0 0.49087611]

skip length N +1 line

read *|plev|area|codice|estr1|estr2|lung|imax|rsd|xsd|bsd1|bsd2|||

skip length N +3 line

create

plev=[L L L L L L L ....] 
area=[I I I I I I I ....]
codice=[D10203 D10304 ...] 
estr1=[N02 N03 N04 N05  ...] 
estr2=[N03 N04 N05...] 
lung=[1.884 1,662 ....] 
imax=[360 360 .....] 
rsd=[number....] 
xsd=[number....] 
bsd1=[number ....] 
bsd2=[number....]

close the cycle 

I hope it is understandable. My biggest problem is counting the N and L in the text.

一紙繁鸢 2024-09-24 07:01:52
function readtest2()

fid = fopen('test2.txt'); 

skipLines(3)
names1 = getNames;
skipLines(1);
nEntries1 = countPrefix('N');
data1 = textscan(fid,'%s %s %d %s %s %d %d %f %f %f %[| ]', nEntries1, 'delimiter','|');


skipLines(2)
names2 = getNames;
skipLines(1);
nEntries2 = countPrefix('L');
data2 = textscan(fid,'%s %s %s %s %s %f %d %f %f %f %f %[| ]', nEntries2, 'delimiter','|');

fclose(fid);

getData(data1, names1);
getData(data2, names2);



function names = getNames()
  names = fgetl(fid);
  names = textscan(names,'%s','delimiter','|');
end

function getData(data, names)

  for i = 1:size(data,2)-1
    values = ( data{i}(1:end));
    if(iscell(values))
      values = cell2mat(values);
    end

    name = names{1}{i+1};

    % very basic error checking
    if(~strcmp(name, ''))

      %save the value in the calling work space
      assignin('base', name, values)
    end
  end

end


function skipLines(n)
  while(n > 0)
    fgetl(fid);
    n = n - 1;
  end
end

function n = countPrefix(prefix)
  pos = ftell(fid);

  n = 0;
  currLine = fgetl(fid);
  while(currLine(1) == prefix)
    currLine = fgetl(fid);
    n = n + 1;
  end
  fseek(fid, pos, 'bof');
end


end
function readtest2()

fid = fopen('test2.txt'); 

skipLines(3)
names1 = getNames;
skipLines(1);
nEntries1 = countPrefix('N');
data1 = textscan(fid,'%s %s %d %s %s %d %d %f %f %f %[| ]', nEntries1, 'delimiter','|');


skipLines(2)
names2 = getNames;
skipLines(1);
nEntries2 = countPrefix('L');
data2 = textscan(fid,'%s %s %s %s %s %f %d %f %f %f %f %[| ]', nEntries2, 'delimiter','|');

fclose(fid);

getData(data1, names1);
getData(data2, names2);



function names = getNames()
  names = fgetl(fid);
  names = textscan(names,'%s','delimiter','|');
end

function getData(data, names)

  for i = 1:size(data,2)-1
    values = ( data{i}(1:end));
    if(iscell(values))
      values = cell2mat(values);
    end

    name = names{1}{i+1};

    % very basic error checking
    if(~strcmp(name, ''))

      %save the value in the calling work space
      assignin('base', name, values)
    end
  end

end


function skipLines(n)
  while(n > 0)
    fgetl(fid);
    n = n - 1;
  end
end

function n = countPrefix(prefix)
  pos = ftell(fid);

  n = 0;
  currLine = fgetl(fid);
  while(currLine(1) == prefix)
    currLine = fgetl(fid);
    n = n + 1;
  end
  fseek(fid, pos, 'bof');
end


end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文