声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1772|回复: 5

[综合讨论] 有什么办法是每次循环都放在一个mat文件里?

[复制链接]
发表于 2011-5-13 11:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
function [a,b]=fx
for r=5:5:20;
    t=0:pi:2*pi;
    x=r*cos(t);
    a=[x];
    save aa a;
    y=r*cos(t);
    b=[y];
    save bb b;
    plot(x,y);
    hold on;
end
hold off
后一次循环的数据覆盖前一次的数据。。只有最后一次的数据是存储在aa文件中。各位大侠有什么办法存储每次循环的数据。
回复
分享到:

使用道具 举报

发表于 2011-5-13 12:32 | 显示全部楼层
你把每次循环命名的文件名aa、bb用变量表示就行
如设为i 每次循环让i=i+1
 楼主| 发表于 2011-5-13 15:10 | 显示全部楼层
回复 2 # wangyouyi 的帖子

function [x,y]=fx
i=1;
j=1;
for r=5:5:20;
    t=0:pi:2*pi;
    x=r*cos(t);
    a=[x];
    save i a;
    i=i+1
    y=r*cos(t);
    b=[y];
    save j b;
    j=j+1;
    plot(x,y);
    hold on;
改成这个样子也不行额。。还是一样的结果。。是改错了吗?
发表于 2011-5-13 20:47 | 显示全部楼层
本帖最后由 tenglang 于 2011-5-13 21:07 编辑

对于mat格式的追加,参考help
具体参考 save 带的参数
  1. Appending to an Existing File

  2. You can add new variables to those already stored in an existing MAT-file by using save -append. When you append to a MAT-file, MATLAB first looks in the designated file for each variable name specified in the argument list, or for all variables if no specific variable names are specified. Based on that information, MATLAB does both of the following:

  3.     *

  4.       For each variable that already exists in the MAT-file, MATLAB overwrites its saved value with the new value taken from the workspace.
  5.     *

  6.       For each variable not found in the MAT-file, MATLAB adds that variable to the file and stores its value from the workspace.

  7.       Note   Saving with the -append option does not append additional elements to any arrays that are already saved in the MAT-file.
复制代码

非高级数据格式的存储参考help中的例子
Export Binary Data with Low-Level I/O
  1. Example — Appending Binary Data to an Existing File

  2. Add the values [55 55 55 55] to the end of the changing.bin file created in the previous example.

  3. % open the file to append and read
  4. fid = fopen('changing.bin','a+');

  5. % write values at end of file
  6. fwrite(fid,[55 55 55 55]);

  7. % read the results from the file into a 4-by-5 matrix
  8. frewind(fid);
  9. appended = fread(fid, [4,5])

  10. % close the file
  11. fclose(fid);

  12. The appended data in the file changing.bin is:

  13.     16    44     3    13    55
  14.      5    44    10     8    55
  15.      9    44     6    12    55
  16.      4    44    15     1    55
复制代码
发表于 2011-5-13 21:26 | 显示全部楼层
回复 3 # lulongy 的帖子

我把你那个程序改了下 代码如下!看看是否满足你的需求
clc
clear
i=1;
for r=5:5:20;
    t=0:pi:2*pi;
    x=r*cos(t);
    a=[x];
    save(num2str(i),'a');
    i=i+1
    y=r*cos(t);
    b=[y];
    save(num2str(i),'b');
    i=i+1;
    plot(x,y);
    hold on;
end

评分

1

查看全部评分

 楼主| 发表于 2011-5-14 22:15 | 显示全部楼层
回复 5 # wangyouyi 的帖子

谢谢!!!!
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-20 05:51 , Processed in 0.059941 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表