将for循环生成的矢量长度不同的输出值保存/追加到MATLAB中的单独文件中 [英] Saving/appending output values of varying vector length generated from for loop into separate files in MATLAB
问题描述
在迭代保存或附加通过for循环内的函数生成的"dist"和"P"获得的输出时,我需要帮助.虽然显示了输出,但是我无法将"dist"和"P"中的各种输出值保存在单独的文件中 没有覆盖.
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.
输入数据在两个Excel文件中提供
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
我使用的邻接矩阵是adj.xlsx
and the adjacency matrix I used is adj.xlsx
我正在使用的matlab代码如下:
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
在matlab工作区中显示的示例输出如下:
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
但是我希望将结果保存在dist和P的单独文件中(结果是向量长度的变化)
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!!
谢谢.
Ashalatha Sreshty 分子生物物理学系 印度科学研究所
Ashalatha Sreshty Molecular Biophysics Unit Indian Institute of Science
推荐答案
您可以使用fopen创建txt对象,然后使用fprintf在此文本文件中写入:
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);
这篇关于将for循环生成的矢量长度不同的输出值保存/追加到MATLAB中的单独文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!