Acquire data from text files in MATLAB
I have a big problem to Acquire a block of data structured in a particular way. Here's how the data are to be acquired (is a txt):
V|0|0|0|t|0|1|1|4|11|T4|H13||||||||||||
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|||||||||||||
*|codserv|area|codice|nome|tnom|tmin|tmax|pc|qc|susc|||||||
*|||||kV|kV|kV|MW|MVAR|S||||||||||||
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|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||||||||
The algorithm should:
skip the first 3 rows
skip fifth row
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]
take a look at textscan
do you have any control over the format of the textfile?
EDIT
here's a rather hackish way to achieve the result
function readtest()
fid = fopen('test.txt');
%skip 3 lines, save 4th, skip 5th
for i = 1:4
names = fgetl(fid);
end
fgetl(fid);
% separate out names
names = textscan(names,'%s','delimiter','|');
% read the data
data = textscan(fid,'%s %s %d %s %s %d %d %f %f %f %[| ]','delimiter','|');
fclose(fid);
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('caller', name, values)
end
end
Any reason for Matlab? If you're in academia, you might have access to LabVIEW, which could be easier to learn for something like this. You'll want to use the Read from Text File VI, then parse the string. Of course, you'll have to make use of the "|" characters to separate the data (use the Match Pattern VI). You might eventually want to restructure the way data are stored to the text file too - use text keys rather than |. Something like:
codserv N area | codice 1 nome N01 tnom 20
etc...
Sorry for not providing an answer with some Matlab source but I would consider LabVIEW if it's an option.
精彩评论