I need help in iteratively saving or appending the output obtained in terms of "dist" and "P" generated through a function inside a for loop. Although, the output is displayed I am unable to save the various output values from "dist" and "P" in separate files without overwriting.
The input data is provide in two excel files
Test1.xlsx
26
50
52
56
58
59
77
78
79
80
Test2.xlsx
51
52
187
188
189
191
226
227
228
229
and the adjacency matrix I used is adj.xlsx
The matlab code I was using is as below:
adj=xlsread('adjmat.xlsx','Sheet1','A1:JO275')
SP1=xlsread('Test1.xlsx','Sheet1','A1:A10');
INHBP=xlsread('Test2.xlsx','Sheet1','A1:A10');
for i=1:length(SP1)
for j=1:length(INHBP)
[dist,P]=dijkstra(adj,SP1(i),INHBP(j))
end
end
The sample output displayed in the matlab workspace is a below:
dist =
27
P =
26 38 40 50 51
dist =
27
P =
26 38 40 50 52
dist =
78
P =
26 38 40 50 52 55 60 63 80 82 116 117 119 187
dist =
88
P =
26 38 40 50 52 55 60 63 80 82 116 117 119 187 188
dist =
98
P =
Columns 1 through 15
26 38 40 50 52 55 60 63 80 82 116 117 119 187 188
Column 16
189
dist =
73
P =
26 38 40 70 71 75 76 108 111 113 136 175 178 180 191
dist =
85
P =
26 38 40 50 52 55 60 63 80 82 116 117 118 198 226
dist =
44
P =
26 38 40 50 52 55 60 63 83 227
dist =
33
P =
26 38 40 50 52 55 60 63 166 228
But I want the results to be save in separate files for dist and P (results are varying length of vectors), for instance
dist =
27
27
78
88
98
73
85
44
33
P =
26 38 40 50 51
26 38 40 50 52
26 38 40 50 52 55 60 63 80 82 116 117 119 187
26 38 40 50 52 55 60 63 80 82 116 117 119 187 188
26 38 40 50 52 55 60 63 80 82 116 117 119 187 188 189
26 38 40 70 71 75 76 108 111 113 136 175 178 180 191
26 38 40 50 52 55 60 63 80 82 116 117 118 198 226
26 38 40 50 52 55 60 63 83 227
26 38 40 50 52 55 60 63 166 228
Please help me resolve this!!
Thank in advance.
Ashalatha Sreshty Molecular Biophysics Unit Indian Institute of Science
One method would be to dynamically generate a formatspec
for fprintf
in each of your calculation iterations based on the length of your P
vector.
Consider the following example:
dfID = fopen('dist.txt', 'w'); % Discards existing contents
pfID = fopen('p.txt', 'w'); % Discards existing contents
l = [4 7 2]; % Dummy variable to define length of our P output per loop iteration
for ii = 1:length(l)
dist = ii;
fprintf(dfID, '%u\n', dist);
P = ones(1, l(ii));
fprintf(pfID, strcat(strtrim(repmat('%u ', 1, length(P))), '\n'), P);
end
% Clean up
fclose(dfID);
fclose(pfID);
Which produces dist.txt
:
1
2
3
Along with p.txt
:
1 1 1 1
1 1 1 1 1 1 1
1 1
You can use fopen to create your txt object and then write in this text file with fprintf:
fid1 = fopen('dist.txt','w');
fid2 = fopen('P.txt','w');
adj=xlsread('adjmat.xlsx','Sheet1','A1:JO275')
SP1=xlsread('Test1.xlsx','Sheet1','A1:A10');
INHBP=xlsread('Test2.xlsx','Sheet1','A1:A10');
for i=1:length(SP1)
for j=1:length(INHBP)
[dist,P]=dijkstra(adj,SP1(i),INHBP(j))
b = sprintf('%d ',P);
fprintf(fid1,'%d\n',dist)
fprintf(fid2,'%s\n',b)
end
end
fclose(fid1);
fclose(fid2);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.