*Import data from a file - HELP*

3 次查看(过去 30 天)
Hi guys! I'm writing about a problem with a code. I explain: I have a .txt file named input_luglio2012.txt, from where I need to import my data. The file is this:
Numero richieste n
7
Numero mezzi m
2
Tempo ritiro/consegna T
20
localita ritiro ri
2 4 6 8 10 12 13
localita consegna ci
3 5 7 11 9 15 14
Tempi tij
0 89 82 126 122 140 143 149 147 144 99 137 74 89 77
89 0 82 124 121 141 142 143 77 143 99 147 122 124 91
82 82 0 90 121 98 142 98 149 142 143 93 122 214 91
126 124 90 0 126 141 146 143 146 148 143 143 122 124 121
122 121 121 126 0 141 146 147 144 146 143 143 122 124 121
140 141 98 141 141 0 140 149 147 149 143 143 144 139 137
143 142 142 146 146 140 0 76 143 143 143 121 144 139 137
149 143 98 143 147 149 76 0 99 146 143 141 144 139 137
147 77 149 146 144 147 143 99 0 99 143 141 144 139 137
144 143 142 148 146 149 143 146 99 0 143 141 144 139 137
99 99 93 143 143 143 143 143 143 143 0 121 144 139 137
137 147 143 143 143 143 121 141 141 141 121 0 94 139 137
74 122 122 122 122 144 144 144 144 144 144 94 0 83 73
89 124 124 124 124 139 139 139 139 139 139 139 83 0 83
77 91 91 121 121 137 137 137 137 137 137 137 73 83 0
I need to have in the workspace:
n=7;
m=2;
T=20;
R=[2 4 6 8 10 12 13];
C=[3 5 7 11 9 15 14];
T=[0 89 82 126 122 140 143 149 147 144 99 137 74 89 77
89 0 82 124 121 141 142 143 77 143 99 147 122 124 91
82 82 0 90 121 98 142 98 149 142 143 93 122 214 91
126 124 90 0 126 141 146 143 146 148 143 143 122 124 121
122 121 121 126 0 141 146 147 144 146 143 143 122 124 121
140 141 98 141 141 0 140 149 147 149 143 143 144 139 137
143 142 142 146 146 140 0 76 143 143 143 121 144 139 137
149 143 98 143 147 149 76 0 99 146 143 141 144 139 137
147 77 149 146 144 147 143 99 0 99 143 141 144 139 137
144 143 142 148 146 149 143 146 99 0 143 141 144 139 137
99 99 93 143 143 143 143 143 143 143 0 121 144 139 137
137 147 143 143 143 143 121 141 141 141 121 0 94 139 137
74 122 122 122 122 144 144 144 144 144 144 94 0 83 73
89 124 124 124 124 139 139 139 139 139 139 139 83 0 83
77 91 91 121 121 137 137 137 137 137 137 137 73 83 0];
How can I proceed? I was thinking about the function "textread", but I have many problems concerning the text format!
Please help me!
Thank you,
Manuele

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-6-20
f = fopen('nameyourtextfile.txt');
cll = textscan(f,'%s');
fclose(f);
c = cll{:};
i1 = ~cellfun(@isempty,regexp(c,'\d*'));
L = bwlabel(i1);
data = accumarray(L(i1),str2double(c(i1)),[],@(x){x'});
data{end} = reshape(data{end},sqrt(numel(data{end})),[]);
var= cellfun(@upper,c(strfind(i1(:)',[0 1])),'un',0);
v2 = strcat(var,',');
eval(['[',[v2{:}],'] = data{:};']);

更多回答(3 个)

Matt Kindig
Matt Kindig 2012-6-20
I would first read the file into a string using fileread(). You should then be able to parse the file using regexp(), with the 'split' option, to separate the text portions from the numeric portions. That should get you started.

Manuele
Manuele 2012-6-20
Hi Matt! First of all I want to thank you for your answer.
I was trying to do what you said, but the problem is that, reading on the Matlab help, I can't find a way to parse and split my input file (imported as you said using fileread) and obtain 6 vectors or matrices that I can reuse.
Can you write me an example code?
Thank you, and sorry for the inconvenient!

Manuele
Manuele 2012-6-21
Andrei this is simply perfect! How can I thank you???

标签

Community Treasure Hunt

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

Start Hunting!

Translated by