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 日
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?
Star Strider
2014 年 6 月 6 日
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.
Star Strider
2014 年 6 月 6 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
