필터 지우기
필터 지우기

How can I save a column in already saved matix in .dat file in Matlab

조회 수: 2 (최근 30일)
talal
talal 2014년 8월 3일
편집: talal 2014년 8월 3일
I am cheema and want to save values that are in matrix form (in column) to a saved matrix in .dat file.
Actually, I can successfully add new values preserving the previous one's when I have only one scalar value, but want to add up a complete column to the saved matrix preserving the previous values. So that I can plot all the values from start to end. The main theme is described in the code:
for i=1:100
k=i+5;
l=i+2;
a=[k l];
b=[k1 l1];
c=abs(a-b);
h=(k+k1)/2;
h1=(h+k1)/2;
h2=(h+k)/2;
h3=(h1+k1)/2;
h4=(h+h1)/2;
h5=(h+h2)/2;
h6=(h2+k)/2;
h7=(h3+k1)/2;
h8=(h1+h3)/2;
h9=(h1+h4)/2;
h10=(h+h4)/2;
h11=(h+h5)/2;
h12=(h5+h2)/2;
h13=(h2+h6)/2;
h14=(h6+k)/2;
g=(l+l1)/2;
g1=(g+l1)/2;
g2=(g+l)/2;
g3=(g1+l1)/2;
g4=(g+g1)/2;
g5=(g+g2)/2;
g6=(g2+l)/2;
g7=(g3+l1)/2;
g8=(g1+g3)/2;
g9=(g1+g4)/2;
g10=(g+g4)/2;
g11=(g+g5)/2;
g12=(g5+g2)/2;
g13=(g2+g6)/2;
g14=(g6+l)/2;
if c>=[0 0] & c<[8 5]
k2=k;
l2=l;
elseif c>=[8 0]& c<[12 5]
k2=[h;k];
l2=[l;l];
elseif c>=[12 0] & c<[25 5]
k2=[h1;h;h2;k];
l2=[l;l;l;l];
elseif c>=[25 0] & c<[50 5]
k2=[h3;h1;h4;h;h5;h2;h6;k];
l2=[l;l;l;l;l;l;l;l];
elseif c>=[0 5] & c<[8 10]
k2=[k;k];
l2=[g;l];
elseif c>=[0 10] & c<[8 25]
k2=[k;k;k;k];
l2=[g1;g;g2;l];
elseif c>=[0 25] & c<[8 50]
k2=[h;h;h;h;h;h;h;h];
l2=[g3;g1;g4;g;g5;g2;g6;l];
elseif c>=[8 5] & c<[12 10]
k2=[h;k];
l2=[g;l];
elseif c>=[8 10] & c<[12 25]
k2=[h;h;k;k];
l2=[g1;g;g2;l];
elseif c>=[8 25] & c<[12 50]
k2=[h;h;h;h;k;k;k;k];
l2=[g3;g1;g4;g;g5;g2;g6;l];
elseif c>=[12 5] & c<[25 10]
k2=[h1;h;h2;k];
l2=[g;g;l;l];
elseif c>=[12 10] & c<[25 25]
k2=[h1;h;h2;k];
l2=[g1;g;g2;l];
elseif c>=[12 25] & c<[25 50]
k2=[h1;h1;h;h;h2;h2;k;k];
l2=[g3;g1;g4;g;g5;g2;g6;l];
elseif c>=[25 5] & c<[50 10]
k2=[h3;h1;h4;h;h5;h2;h6;k];
l2=[g;g;g;g;l;l;l;l];
elseif c>=[25 10] & c<[50 25]
k2=[h3;h1;h4;h;h5;h2;h6;k];
l2=[g1;g1;g;g;g2;g2;l;l];
elseif c>=[25 25] & c<[50 50]
k2=[h3;h1;h4;h;h5;h2;h6;k];
l2=[g3;g1;g4;g;g5;g2;g6;l];
else
k2=[h7;h3;h8;h1;h9;h4;h10;h;h11;h5;h12;h2;h13;h6;h14;k];
l2=[g7;g3;g8;g1;g9;g4;g10;g;g11;g5;g12;g2;g13;g6;g14;l];
end
q(i,:)=[k2 l2];
save path.dat q -ascii;
x=path(:,1);
y=path(:,2);
plot(x,y,'-o')
k1=k;
l1=l;
end
I am getting error:
In an assignment A(I) = B, the number of elements in B and I must be the same. q(i,:)=[k2 l2];
I would be very thankful for any suggestion.
  댓글 수: 1
talal
talal 2014년 8월 3일
편집: talal 2014년 8월 3일
actually I have saved vector matrix like[1;2;3;4;5;6;7] in a dat file and i want to add another vector matrix like [8;9]in a same sequence so that saved matrix would be [1;2;3;4;5;6;7;8;9]

댓글을 달려면 로그인하십시오.

답변 (1개)

dpb
dpb 2014년 8월 3일
편집: dpb 2014년 8월 3일
In an assignment A(I) = B, the number of elements in B and I must be the same. q(i,:)=[k2 l2];
You've got a bunch of different k2, l2 vectors that you're attempting to concatenate horizontally. No can do unless they're all the same length as the error message is telling you.
You can only put the longer/shorter in the same numeric array if you somehow pad/truncate all to be the same length; Matlab can only handle "jagged" arrays via cell arrays.
If the primary purpose is to plot, two basic choices --
a) plot the first vector, then use hold on and add the subsequent in individual calls, fixing up colors/linestyles, etc., as desired, or ;
b) allocate the q array to it's maximum size initially with q=nan(nRow,nCol); and then use the actual length in the assignment as
...
L=length([k2 l2]);
q(i,1:L)=[k2 l2];
...
plot has a nice feature that it will simply quietly ignore the NaN entries and deliver the desired plot automagically.
ADDENDUM
In solution b), would be far "more better" efficiency-wise to only do the concatenation once via a temporary. I suppose it's possible the JIT optimizer can do this on its own but I'd kinda' doubt it. If the vectors aren't terribly long it won't really make a lot of difference, but still...
...
V=[k2 l2];
L=length(V);
q(i,1:L)=V;
...
  댓글 수: 1
talal
talal 2014년 8월 3일
thanks but it is not working may be i did not explain you well. actually I have saved vector matrix like[1;2;3;4;5;6;7] and i want to add another vector matrix like [8;9]in a same sequence so that saved matrix would be [1;2;3;4;5;6;7;8;9]

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by