필터 지우기
필터 지우기

Saving a matrix from a function file

조회 수: 7 (최근 30일)
Aakar
Aakar 2014년 1월 14일
댓글: Mischa Kim 2014년 1월 14일
Hi, I have created a function file and I need one of the values from the middle of that function file to plot it against time.
That function file is in a continuous loop, but when I save that one particular matrix it saves only the first value and plots a straight line against time. I cant figure out how I can create a plot with the varying value for that variable
This is the command that I am using to save that matrix. 'x', 'y' and 'z' are all variables. 'x' changes with time.
F = x*y + z;
save('F.mat','F');
Do i use a while loop or something else?

답변 (3개)

Mischa Kim
Mischa Kim 2014년 1월 14일
편집: Mischa Kim 2014년 1월 14일
Could you share more of your code to see what exactly is happening, maybe as an attachment (see paper clip)?
Note, that in the above code F is a scalar, not a matrix. To make it a matrix (or vector) use (e.g.)
for ii = 1:n
...
F(ii) = x(ii)*y + z;
...
end
assuming that x is a vector and y a scalar.

David Sanchez
David Sanchez 2014년 1월 14일
Use dot notation to operate with arrays:
x = rand(10,1);
y = rand(10,1);
z = rand(10,1);
F = x.*y + z; % MIND THE DOT
save('F.mat','F');
F =
0.5858
0.6918
0.8671
0.1993
0.1371
0.9017
1.2694
0.5453
1.2593
0.2498

Aakar
Aakar 2014년 1월 14일
Thanks for your replies. I understand the overall concept of vectors and arrays. I am still not being able to save the different values of F as it is within a function file. Even if I run a 'for loop' in it, the value still will remain the same.
x is obtained from a ODE solver and it calls this function. So the value of x changes once every cycle. Therefore the value of F should change every cycle.
I am sorry, I wont be able to share more of the code as there are a lot variables and scalar values.
  댓글 수: 1
Mischa Kim
Mischa Kim 2014년 1월 14일
Just attach (paper clip icon) your code as a whole to your question.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by