How can I creat a Excel Table Data from a not really well formated Text Data

1 view (last 30 days)
Dear Matlab comunity! As it asked in the question, I have a file Textdata.txt looks like this:
object1
xmin = -20.2
xmax = 30.22E-2
ymin = -10.35274
ymax = 40.5723
zmin = 50.38
zmax = 60.1342
end Objekt1
objekt2
xmin = .....
xmax = .....
ymin = .....
ymax = .....
zmin = ......
zmax = .....
end object2
object3...
object4...
...
objectn...
and i want to write a matlab progamm, which creats an Excel Data Table from the file Textdata.txt. The Excel Table should look like:
xmin xmax ymin ymax zmin zmax
object1 ... ... ... ... ... ...
object2 ... ... ... ... ... ...
.
.
.
objectn ... ... ... .. ... ...
I have tried the function "fscanf" but not yet successful. Thank you verry much!

Accepted Answer

Friedrich
Friedrich on 6 Jun 2013
Edited: Friedrich on 6 Jun 2013
Hi,
there is an example in the doc which matches your format quite perfectly:
So something like this:
fid = fopen('test.txt','r')
tmp = textscan(fid,'%s xmin = %f xmax = %f ymin = %f ymax = %f zmin = %f zmax = %f %*s %*s','delimiter','\n')
fclose(fid);
And then you can do something like:
data = cell(numel(tmp{1}),numel(tmp));
for i=1:numel(tmp)
if iscell(tmp{i})
data(:,i) = tmp{i};
else
data(:,i) = num2cell(tmp{i});
end
end
disp(data)
xlswrite('test.xls',data)
  6 Comments
Friedrich
Friedrich on 10 Jun 2013
Edited: Friedrich on 10 Jun 2013
No, at least not for my test example:
object1
xmin = -20.2
xmax = 30.22E-2
ymin = -10.35274
ymax = 40.5723
zmin = 50.38
zmax = 60.1342
end Objekt1
objekt2
xmin = 1
xmax = 2
ymin = 3
ymax = 4
zmin = 5
zmax = 6
end object2
When using the code above I get for disp(data)
>>disp(data)
Columns 1 through 6
'object1' [-20.2000] [0.3022] [-10.3527] [40.5723] [50.3800]
'objekt2' [ 1] [ 2] [ 3] [ 4] [ 5]
Column 7
[60.1342]
[ 6]

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!