creating subplot from function
12 views (last 30 days)
Show older comments
I have some code, generally looks like this:
x='dr_0.01.txt';
foo(x);
function y = foo(fname)
Do stuff
end
Ultimately, the function creates a figure. How can I load in other files with the x variable, run foo(x), and put each figure created from each call to function f into a subplot that is 2x2? So I want to load in 4 files, and call the function on all 4 files. Then create a subplot that contains all 4.
0 Comments
Answers (1)
Chad Greene
on 7 Nov 2018
Looks like instead of x you meant to call that variable fname? If so, something like this:
fname = {'fname1.txt','fname2.txt','fname3.txt','fname4.txt'};
function y = foo(fname)
% Load and plot each dataset:
for k = 1:length(fname)
D = importdata(fname{k});
subplot(2,2,k)
y(k) = plot(D(:,1),D(:,2))
end
end
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!