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))
Explore the options in the documentation for 2-D and 3-D Plots for all the details.
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!

Translated by