1 | % read_lvm: read data from the output files of labview (file extension .lvm) |
---|
2 | function Data=read_lvm(filename) |
---|
3 | Data.ListGlobalAttribute={'FileName','Experiment','DateTime'}; |
---|
4 | [Path,Data.FileName] = fileparts(filename);% record the file name |
---|
5 | [tild,Data.Experiment]=fileparts(Path);% record the experient name |
---|
6 | |
---|
7 | %% read the full text of the file as a char string txt |
---|
8 | txt = fileread(filename); |
---|
9 | |
---|
10 | %% get time string (date and time of the experiment) |
---|
11 | |
---|
12 | Date_pos=regexp(txt,'Date\s','once');%find the char string 'Date' followed by a blank |
---|
13 | txt(1:Date_pos+length('Date'))=[]; %remove the header until 'Date'; |
---|
14 | DateString=txt(1:regexp(txt,'\n','once')-1);% read char until the next line break |
---|
15 | r1=regexp(DateString,'(?<DateDat>\S+)','names');% keep the non blank string |
---|
16 | Time_pos=regexp(txt,'Time\s','once');%find the char string 'Time' followed by a blank |
---|
17 | txt(1:Time_pos+length('Time'))=[]; %remove the header until 'Time'; |
---|
18 | TimeString=txt(1:regexp(txt,'\n','once')-1);% read char until the next line break |
---|
19 | r2=regexp(TimeString,'(?<TimeDat>\S+)','names');% keep the non blank string |
---|
20 | TimeString=regexprep(r2.TimeDat,',','.');% replace ',' by '.' |
---|
21 | Dot_pos=regexp(TimeString,'\.'); |
---|
22 | TimeString=TimeString(1:Dot_pos+2); % round to 1/100 s |
---|
23 | r1.DateDat=regexprep(r1.DateDat,'/','-');%replace '/' by '-' (to get standard date representation recognized by Matlab) |
---|
24 | Data.DateTime=[r1.DateDat ' ' TimeString];%insert date to the time string (separated by a blank) |
---|
25 | |
---|
26 | %% remove header text |
---|
27 | Header_pos=regexp(txt,'***End_of_Header***','once');%find the first '***End_of_Header***' |
---|
28 | txt(1:Header_pos+length('***End_of_Header***')+1)=[];%remove header |
---|
29 | Header_pos=regexp(txt,'***End_of_Header***','once');%find the second '***End_of_Header***' |
---|
30 | txt(1:Header_pos+length('***End_of_Header***')+1)=[];%remove header |
---|
31 | title_pos=regexp(txt,'\S','once');% find the next non blank char |
---|
32 | txt(1:title_pos-1)=[];% remove the blank char at the beginning |
---|
33 | |
---|
34 | %% get the list of channel names |
---|
35 | Break_pos=regexp(txt,'\n','once');%find the line break |
---|
36 | VarNameCell=textscan(txt(1:Break_pos-1),'%s');% read list of variable names (until next line break) |
---|
37 | Data.ListVarName=VarNameCell{1}; |
---|
38 | Data.ListVarName(end)=[]; %remove last name (Comment) |
---|
39 | Data.ListVarName{1}='Time'; %replace first name ('X_Value') by 'Time') |
---|
40 | NbChannel=numel(Data.ListVarName); |
---|
41 | for ivar=1:NbChannel |
---|
42 | Data.VarDimName{ivar}='nb_sample'; |
---|
43 | end |
---|
44 | |
---|
45 | %% get the data |
---|
46 | txt(1:Break_pos-1)=[];%removes line of channel names |
---|
47 | txt=regexprep(txt,',','.');%replace comma by dots (French to English notation) |
---|
48 | txt=textscan(txt,'%s');% transform txt in a cell of strings |
---|
49 | txt=reshape(txt{1},NbChannel,[]); |
---|
50 | txt=cellfun(@str2double,txt);% transform char to a matrix of numbers |
---|
51 | txt=txt'; %transpose matrix |
---|
52 | for ivar=1:NbChannel |
---|
53 | Data.(Data.ListVarName{ivar})=txt(:,ivar); |
---|
54 | end |
---|
55 | |
---|
56 | %% calculate position in case of a non-zero motor signal |
---|
57 | % To plot profiles(e;g.for C5): plot(Data.Position(Data.Speed<0),Data.C5(Data.Speed<0)) |
---|
58 | SpeedDown=-1; %motot speed 1 cm/s |
---|
59 | SpeedUp=1; %motot speed 1 cm/s |
---|
60 | if isfield(Data,'Motor_profile')% Motor_profile signal =0 (no motion), -5 (down), +5(up) |
---|
61 | Data.ListVarName=[Data.ListVarName' {'Position','Speed'}]; |
---|
62 | Data.VarDimName=[Data.VarDimName {'nb_sample','nb_sample'}]; |
---|
63 | Speed=zeros(size(Data.Motor_profile)); |
---|
64 | if ~isempty(find(Data.Motor_profile>2.5|Data.Motor_profile<-2.5)) |
---|
65 | Speed(Data.Motor_profile>2.5)=SpeedUp;% threshold at 2.5 to avoid noise effects |
---|
66 | Speed(Data.Motor_profile<-2.5)=SpeedDown; |
---|
67 | Data.Speed=Speed; |
---|
68 | Speed(end)=[]; |
---|
69 | Data.Position=[0; cumsum(Speed.*diff(Data.Time))]; |
---|
70 | end |
---|
71 | end |
---|
72 | |
---|