tags:

views:

65

answers:

2

Hello everyone. 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:

1)skip the first 3 rows 2)skip fifth row

2)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=[]

3)Fill vettors 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]

Thanks in advance for your help and sorry for my English

A: 

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.

gary comtois
This is part of a project where they are bound to have to use matlab. In fact the data will be acquired in an algorithm in MATLAB. I also would have preferred use Labview but i can't.
Marietto85
OK, well you may want to replace the |'s with tabs (or commas). Then open the text file as a tab-deliminted matrix (or CSV if you use commas). Best of luck.
gary comtois
+2  A: 

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
second
I agree... restructuring the format may be required if staying in Matlab.
gary comtois
I think restructuring the format may be required to make the task easier in any language - even with LabVIEW.
Jonas
I thank you for the help I'm trying the solution you made. you really are a great programmer second!!!
Marietto85
Unfortunately I have no control over the file because it is a text format but generated by a C program and the program is an executable (. exe)
Marietto85
"second" my mail is [email protected] .email or msn we can talk, the problem I have is small but difficult to explain well.
Marietto85