Reputation: 1043
I'm trying to accomplish a relatively simple task of reading an ASCII data file into MATLAB. The file structure is given below:
(0,180)
[ 0 0.0174533 0.0349066 0.0523599 0.0698132 ... ]
While I'm able to read in the first line with fscanf
I fail to read in the vector of float values with an '%f'
:
A = fscanf(fid, '(%d,%d)\n[ %f ]').
I have figured out a solution to my problem. Two calls to textscan
must be used instead of a single call to fscanf
. More elegant solution is always welcome.
function [range, theta] = readTheta(fname)
fid = fopen(fname, 'r');
line1 = fgetl(fid);
C = textscan(line1, '(%d,%d)\n');
range = [ C{1}, C{2} ];
line2 = fgetl(fid);
line2 = regexprep(line2, {'[ ' ' ]'}, '');
C = textscan(line2, '%f');
theta = C{1};
fclose(fid);
end
Upvotes: 0
Views: 301
Reputation: 12345
You can push textscan
a lot harder than this. For example, consider the following parameters settings to textscan.
data = textscan(anyLine,'%f','delimiter','[]() ,','MultipleDelimsAsOne',true);
Now this command will work for either of the lines above.
anyLine= '(0,7)';
data = textscan(anyLine,'%f','delimiter','[]() ,','MultipleDelimsAsOne',true);
disp(data{1});
anyLine= '[ 0 0.0174533 0.0349066 0.0523599 0.0698132 ]';
data = textscan(anyLine,'%f','delimiter','[]() ,','MultipleDelimsAsOne',true);
disp(data{1});
Upvotes: 2