read textfile in Matlab -
i quite stuck matlab problem here.. have *.txt file looks this:
1
2
2
x50
2
2
2
x79
which means @ coordinates (1,2,2) f(x)-value 50 , @ coordinates (2,2,2) f(x)-value 79. trying read matlab have vector (or using repmat meshgrid-like matrix) x , 1 y. not need z since not change on process. want read in f(x)-value can plot whole thing using surf().
if use
[a] = textread('test.txt','%s')
it gives me whole thing... can give me idea please? thinking putting thing in loop, pseudocode
i=1 50 xpos = read first line ypos =read next line zpos = read next line (or ignore.. however..) functionvalue= read next line end
any hints? thanks
assuming data setup in text file lines 1,2,3 xyz coordinate points , next line (fourth line) function value. 5,6,7 next set of xyz coordinate points followed function value on 8th line set , on such repeating format, see if works -
%// read data text file text_data = textread(inputfile,'%s') data = reshape(text_data,4,[]) %// numeric data data(end,:) = strrep(data(end,:),'x','') %// or data(end,:) = arrayfun(@(n) data{end,n}(2:end),1:size(data,2),'uni',0) data_numeric = str2double(data) %// separate xyz , function values xyz = data_numeric(1:3,:)' %//'# each row hold set of xyz coordinates f_value = data_numeric(end,:) %// function values
a bit more robust approach -
%// read data text file txtdata = textread(inputfile,'%s'); %// ----------- part i: xyz --------------------- %// find cell positions first character digit indicating %// these cells containing coordinate points digits_pos_ele = isstrprop(txtdata,'digit'); digits_pos_cell = arrayfun(@(x) digits_pos_ele{x}(1),1:numel(digits_pos_ele)); %// convert numeric format , reshape have each row holding each set %// of xyz coordinates xyz_vals = reshape(str2double(txtdata(digits_pos_cell)),3,[])'; %//' %// ----------- part ii: function values --------------------- %// find positions cell start `x` indicating these %// function value cells x_start_pos = arrayfun(@(n) strcmp(txtdata{n}(1),'x'),1:numel(txtdata)); %// collect function value cells , find function value %// themeselves excluding first character cells f_cell = txtdata(x_start_pos); f_vals = str2double(arrayfun(@(n) f_cell{n}(2:end), 1:numel(f_cell),'uni',0))'; %//' %// error checking if size(xyz_vals,1)~=size(f_vals,1) error('woops, not right!') end
Comments
Post a Comment