lulongy 发表于 2011-5-13 11:15

有什么办法是每次循环都放在一个mat文件里?

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

wangyouyi 发表于 2011-5-13 12:32

你把每次循环命名的文件名aa、bb用变量表示就行
如设为i 每次循环让i=i+1

lulongy 发表于 2011-5-13 15:10

回复 2 # wangyouyi 的帖子

function =fx
i=1;
j=1;
for r=5:5:20;
    t=0:pi:2*pi;
    x=r*cos(t);
    a=;
    save i a;
    i=i+1
    y=r*cos(t);
    b=;
    save j b;
    j=j+1;
    plot(x,y);
    hold on;
改成这个样子也不行额。。还是一样的结果。。是改错了吗?

tenglang 发表于 2011-5-13 20:47

本帖最后由 tenglang 于 2011-5-13 21:07 编辑

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

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:

    *

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

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

      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/OExample — Appending Binary Data to an Existing File

Add the values to the end of the changing.bin file created in the previous example.

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

% write values at end of file
fwrite(fid,);

% read the results from the file into a 4-by-5 matrix
frewind(fid);
appended = fread(fid, )

% close the file
fclose(fid);

The appended data in the file changing.bin is:

    16    44   3    13    55
   5    44    10   8    55
   9    44   6    12    55
   4    44    15   1    55

wangyouyi 发表于 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=;
    save(num2str(i),'a');
    i=i+1
    y=r*cos(t);
    b=;
    save(num2str(i),'b');
    i=i+1;
    plot(x,y);
    hold on;
end

lulongy 发表于 2011-5-14 22:15

回复 5 # wangyouyi 的帖子

谢谢!!!!
页: [1]
查看完整版本: 有什么办法是每次循环都放在一个mat文件里?