将数据和矩阵元素写入文本文件中的问题

发布于 2025-02-07 15:24:30 字数 636 浏览 2 评论 0原文

我在将数据写入MATLAB中的文件时有问题,我有一个LP问题,即他们的解决方案是尺寸n的向量,我想在文本文件中写下有关此解决方案的一些信息,以随时阅读它, (我不想在MATLAB中保存工作空间) 我编写以下代码

n = 5;
Length = 10;
s = [1;0;1;0];
t = 0.6;
Test = fopen('Test.txt', 'w');
fprintf(Test,'TSP Problem Size is: %d \n', n);
fprintf(Test,'Optimal Length is: %.5f \n', Length);
fprintf(Test,'Time To Solve in seconds is: %f \n', t);
fprintf(Test,'Solution is: \n');
for i=1:size(s) 
   fprintf(Test,'%.0f\n', s(i));
end
fclose(Test);

,并且执行“ type(test.txt);”它看起来像我想要的 但是,当我在Windows Explorer中打开文件时,它似乎是一行,我不知道为什么??? 有什么方法可以继续格式化并在单独的行中编写每个值的解决方案 (我尝试DLMWRITE,但它只写矩阵,我需要写一些信息,然后写一些信息)

谢谢您的帮助

I have a problem with writing data to a file in Matlab, I have an LP problem that their solution is a vector of size n, and I want to write some information about this solution in a text file to read it any time I want,
(I don not want to save workspace in matlab)
I write the following code

n = 5;
Length = 10;
s = [1;0;1;0];
t = 0.6;
Test = fopen('Test.txt', 'w');
fprintf(Test,'TSP Problem Size is: %d \n', n);
fprintf(Test,'Optimal Length is: %.5f \n', Length);
fprintf(Test,'Time To Solve in seconds is: %f \n', t);
fprintf(Test,'Solution is: \n');
for i=1:size(s) 
   fprintf(Test,'%.0f\n', s(i));
end
fclose(Test);

and when I execute 'type(Test.txt);' it appears as I want
but when I open file in windows explorer it appears all as one line, I don't know why ???
is there any way to keep formatting and to write the solution every value in a separate line
(I try dlmwrite but it writes only matrix, I need to write some information then the matrix)

thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文