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

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

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

Hi, there ist a pronlem with the Ending of each Objectblock : "end Object1". Is consists 2 String,so you write two times "%*s" to tell the "textscan" to ignore these two Strings. But it does not work. It works verry well In the Example in the tutorial, where there is only one String in the begin and Blockbegin and Blockending. Could you please help me!
What is the actual line when an Objects ends? If it like in your question then this shold work, at least it does for me.
Hi , thanks for your concern. There ist a white line between the blocks of each object.like this :
...
end object1
(white line)
object2
...
Could it be the reason for my Problem ?
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]
yes, you are right. I found the mistake in my code.Thanks !

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!