For loop update matrix
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
In my for loop i want to create and append the elements in a column vector everytime i run the function.
채택된 답변
Star Strider
2014년 6월 6일
1 개 추천
From my previous Answer to your similar question:
for k2 = 1:2 % Simulate two function calls
a = 30; % Input argument
b = 400; % Input argument
k1i = 0; % First line of actual function
filex = 0; % File doesn’t exist at first
filename = 'input_data_file.mat';
if exist(filename,'file') % If file exists, load it
load(filename)
k1i = mtrx(end,1); % Find previous end index value
filex = 1;
end
for k1 = k1i+1:(k1i+a) % Loop to create this version of ‘mtrx’
mtrx(k1,:) = [k1 b];
end
if filex == 1 % If file exists, append to it, if not create it
save(filename, 'mtrx', '-append')
elseif filex == 0
save(filename, 'mtrx')
end % Last line of function (other than its own ‘end’ statement)
fout = fopen('input_data_textfile.txt', 'w+');
fprintf(fout, '%d ', mtrx') % Write text file
end
댓글 수: 6
Victor
2014년 6월 6일
How would i implement this inside a function?
The code as an example function:
function mtx = matfileappend(a,b)
k1i = 0; % First line of actual function
filex = 0; % File doesn’t exist at first
filename = 'input_data_file.mat';
if exist(filename,'file') % If file exists, load it
load(filename)
k1i = mtrx(end,1); % Find previous end index value
filex = 1;
end
for k1 = k1i+1:(k1i+a) % Loop to create this version of ‘mtrx’
mtrx(k1,:) = [k1 b];
end
if filex == 1 % If file exists, append to it, if not create it
save(filename, 'mtrx', '-append')
elseif filex == 0
save(filename, 'mtrx')
end % Last line of function (other than its own ‘end’ statement)
mtx = mtrx; % Returns accumulated matrix ‘mtrx’ as output
end
If you want to write the text file, you could do that inside or outside the function. I omitted it from the function because it’s not necessary there. This also assumes the file name for the mat-file never changes, and is always a part of the function. You could add it as an argument if you want, but that would require some minor changes in the code to define it as the ‘filename’ variable.
The function statement (first line, declaring it as a function) can be change to do what you want it to.
Victor
2014년 6월 6일
That worked perfectly! Thanks! and from that .mat file, how do i plot individual columns? Like lets say I want to plot b in terms of increasing time. Or maybe if i have more arguments, i want to plot c, d, e all separately in terms of time.
My pleasure!
If one of the columns in the matrix is a time vector (years, months, ..., seconds), convert it to date numbers (use the datenum function and the datetick function to convert them into readable form on your x-axis) and plot your variables as:
figure(1)
plot(time, b)
datetick('x', dateformat)
The dateformat can be whatever you want it to be. See the documentation for datetick for details.
You can plot them each in a separate figure just as I did here, or you can plot them together:
figure(2)
plot(time, mtrx(:, 2:end))
datetick('x', dateformat)
I assume here that time is also a column vector.
If you want to plot each of them against the index values in column 1, that becomes for column 2:
figure(3)
plot(mtrx(:,1), mtrx(:,2))
and to plot them all against column 1:
figure(4)
plot(mtrx(:,1), mtrx(:,2:end))
Victor
2014년 6월 6일
For those plots, how do i control the spacing if i were to use the subplot function?
Star Strider
2014년 6월 6일
I saw your Question on that and submitted an Answer. It has been the subject of several recent posts here, so I referred you to the best of those. There are also several topics in the Examples section of the documentation page for subplot.
The idea is to use the 'Position' property in each subplot and adjust it individually for each subplot.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
