From: Guangping Zhang on 28 Jan 2010 23:38 Dear All, I'm a new Matlab people and need your help. I do have multiple variables, A1, A2, A3... in one .mat file for analysis. The matrix A1,A2,A3 have same size. I have another B1,B2,B3...I will do this: A1-B1,A2-B2,A3-B3,... and analyze all of them. How can I write a for loop to do it? I write one, but it never work, like this: for i=1:30 Ci=Ai+Bi end I know Ai is a string, but I don't know how to use eval function to do it. I will appreciate any help. zgp480
From: kinor on 29 Jan 2010 02:55 "Guangping Zhang" <zgp480(a)gmail.com> wrote in message <hjtonc$l7a$1(a)fred.mathworks.com>... > Dear All, > > I'm a new Matlab people and need your help. > I do have multiple variables, A1, A2, A3... in one .mat file for analysis. The matrix A1,A2,A3 have same size. I have another B1,B2,B3...I will do this: > > A1-B1,A2-B2,A3-B3,... > > and analyze all of them. How can I write a for loop to do it? I write one, but it never work, > like this: > for i=1:30 > Ci=Ai+Bi > end > > I know Ai is a string, but I don't know how to use eval function to do it. > > I will appreciate any help. > > zgp480 help eval
From: us on 29 Jan 2010 03:36 "Guangping Zhang" <zgp480(a)gmail.com> wrote in message <hjtonc$l7a$1(a)fred.mathworks.com>... > Dear All, > > I'm a new Matlab people and need your help. > I do have multiple variables, A1, A2, A3... in one .mat file for analysis. The matrix A1,A2,A3 have same size. I have another B1,B2,B3...I will do this: > > A1-B1,A2-B2,A3-B3,... > > and analyze all of them. How can I write a for loop to do it? I write one, but it never work, > like this: > for i=1:30 > Ci=Ai+Bi > end > > I know Ai is a string, but I don't know how to use eval function to do it. > > I will appreciate any help. > > zgp480 one of the solutions - do NOT do it this way % the data % - create a MAT-file (should never do it this way...)... fnam='foo.mat'; % <- your file name a1=1:2; a2=3:4; a3=5:6; save(fnam,'a1','a2','a3'); clear a*; % the engine m=load(fnam); m=struct2cell(m); m=cat(1,m{:}); % <- since your data has the same size... disp(m); %{ 1 2 % <- a1 3 4 % <- a2 5 6 % <- ... %} % - now, use indexing, which is especially helpful for a loop construct, eg, r=m(1,:)-m(2,:) % <- a1-a2 % r = -2 -2 us
From: Sebastian Hölz on 29 Jan 2010 03:59 Easy way without eval: % 'file.mat'contains A1, B1, ..., An, Bn, and is loaded into structure dat = load('file.mat'); % Now iterate over all fields Ai and Bi n = length(fieldnames(dat))/2; for i=1:n dat.(sprintf('C%i',i)) = dat.(sprintf('A%i',i))+dat.(sprintf('B%i',i)); % This is dat.Ci = dat.Ai+dat.Bi, see help for structs end Sebastian
From: Guangping Zhang on 30 Jan 2010 13:00 I really appreciate the help from all the response. the solution works so good. Yours, Guangping, "Sebastian Hölz" <seppel_mit_pp(a)web.de> wrote in message <hju80o$6i8$1(a)fred.mathworks.com>... > Easy way without eval: > > % 'file.mat'contains A1, B1, ..., An, Bn, and is loaded into structure > dat = load('file.mat'); > > % Now iterate over all fields Ai and Bi > n = length(fieldnames(dat))/2; > for i=1:n > dat.(sprintf('C%i',i)) = dat.(sprintf('A%i',i))+dat.(sprintf('B%i',i)); > % This is dat.Ci = dat.Ai+dat.Bi, see help for structs > end > > Sebastian
|
Pages: 1 Prev: Lowpass Butterworth Filter Next: Help in MATLAB.. Normal Probability Plot |