Matlab - importing ascii data -


i ve got ascii file , im trying import matlab in order make plots. there way of importing data, tho contain , (comma) rather . (dot)?

00:00:00,000;-2,14; 00:00:00,001;-1,80; 

well first column want create referred time , corresponding 00:00:00,001; 00:00:00,002; etc. second column should amplitude of sample i.e. -2,14; -1,80 etc.

yup. first use importdata can read each row of text file cell in cell array. after, allow processing of times performed in matlab, you'll need replace each , character .. allow use matlab's commands date , time processing. specifically, use regular expressions this. regular expressions find patterns in strings. can use these patterns extract out data need. use regexprep replace , characters ..

for purposes of answer, example data i'm going using is:

00:00:00,000;-2,14; 00:00:00,001;-1,80; 00:00:00,002;-0,80; 00:00:00,003;2,40; 00:00:00,004;3,78; 

therefore, assuming data stored in text file called data.txt, do:

%// load in each row cell array = importdata('data.txt');  %// each row has , replaced . arep = regexprep(a, ',', '\.'); 

now, can split of quantities using ; delimiter. can use regexp split quantities. can further decompose data by:

arep_decomp = regexp(arep, '[^;]+', 'match'); 

the first parameter cell array contains each of our rows in text file (with commas converted periods). second parameter pattern specifies you're trying in each string in cell array. [^;]+ means want find strings consist of bunch of characters excluding until hit semi-colon. once hit semi-colon, stop. 'match' means want retrieve actual strings stored cell arrays.

the result after above line's execution gives:

arep_decomp{1}{1} =  00:00:00.000   arep_decomp{1}{2} =  -2.14   arep_decomp{2}{1} =  00:00:00.001   arep_decomp{2}{2} =  -1.80   arep_decomp{3}{1} =  00:00:00.002   arep_decomp{3}{2} =  -0.80   arep_decomp{4}{1} =  00:00:00.003   arep_decomp{4}{2} =  2.40   arep_decomp{5}{1} =  00:00:00.004   arep_decomp{5}{2} =  3.78 

you can see output cell array, arep_decomp 5 element cell array, each cell nested 2 element cell array, first element time, , second element magnitude. note these all strings.

what can create 2 numeric arrays convert these quantities numeric representations. specifically, time format have looks form:

hh:mm:ss.fff 

h hours, m minutes, s seconds , f microseconds. use datenum allow convert these time representations actual date numbers. can plot these on graph, perhaps want display these times on plot well. can done manipulating plot functions. nevertheless, use cellfun can extract out time strings separate array can use plotting later, , use convert time strings date numbers via datenum, , convert magnitude numbers actual numbers.

therefore:

datestr = cellfun(@(x) x{1}, arep_decomp, 'uni', 0); datenums = cellfun(@(x) datenum(x, 'hh:mm:ss.fff'), datestr); mags = cellfun(@(x) str2double(x{2}), arep_decomp); 

the first line of code extracts out each of time strings single cell array - uni=0 flag important this. next, convert each time string date number, , convert magnitude strings physical numbers str2double.

now, have plot data. can done by:

plot(datenums, mags);  set(gca, 'xtick', datenums); set(gca, 'xticklabel', datestr); 

the above code plots data date numbers on horizontal axis, magnitude numbers on vertical axis, want rename horizontal axis time strings wanted. therefore, use calls set ensure ticks visible date numbers themselves, , relabel date numbers string representations of times themselves.

once run above code, get:

enter image description here

because time step in between times small, may clutter horizontal axis labels long, yet interval short. therefore, may consider displaying times @ interval , can doing like:

step_size = 5; plot(datenums, mags);  set(gca, 'xtick', datenums(1:step_size:end)); set(gca, 'xticklabel', datestr(1:step_size:end)); 

step_size controls how many ticks , labels appear in succession. obviously, need make sure step_size smaller total number of points in data.


for copying , pasting pleasure, full code wrote looks like:

%// load in each row cell array = importdata('data.txt');  %// each row has , replaced . arep = regexprep(a, ',', '\.');  arep_decomp = regexp(arep, '[^;]+', 'match');  datestr = cellfun(@(x) x{1}, arep_decomp, 'uni', 0); datenums = cellfun(@(x) datenum(x, 'hh:mm:ss.fff'), datestr); mags = cellfun(@(x) str2double(x{2}), arep_decomp);  step_size = 1; %step_size = 5; plot(datenums, mags);  set(gca, 'xtick', datenums(1:step_size:end)); set(gca, 'xticklabel', datestr(1:step_size:end)); 

Comments

Popular posts from this blog

angularjs - Showing an empty as first option in select tag -

qt - Change color of QGraphicsView rubber band -

c++ - Print Preview in Qt -