For loop update matrix
显示 更早的评论
In my for loop i want to create and append the elements in a column vector everytime i run the function.
采纳的回答
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 个评论
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.
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))
For those plots, how do i control the spacing if i were to use the subplot function?
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)
