MATLAB Answers

Combine graphs from different script (directory)

39 views (last 30 days)
Sun Kyoo Choi
Sun Kyoo Choi on 10 Mar 2020
Commented: Sun Kyoo Choi on 11 Mar 2020
Hi all,
I have a queston about plotting, basically about combining multiple graphs from different scripts or directory.
Currently, I have a total of four graphs from different script, and I have to combine all of them in one, however, I cannot do it because they have their own directory.
For example,
Monday = dir('/Users/jameschoi/Desktop/ME/Monday/*.xls');
Tuesday = dir('/Users/jameschoi/Desktop/ME/Tuesday(1)/*.xls');
Wednesday = dir('/Users/jameschoi/Desktop/ME/Wednesday/*.xls');
Thursday = dir('/Users/jameschoi/Desktop/ME/Thursday/*.xls');
I have these four directory and four individual m files. An individual m file is saved in Mon - Thurs folders respectively, and I have an individual graph for Mon,Tues,Wed, and Thurs as well, and my ultimate goal is to add them up.
In order to do it, do I have to make 5th script and call the data?, Or is there another easy way to do it?
If I have to call a data, could you please provide a sample code for it?
Thank you.

  0 Comments

Sign in to comment.

Answers (1)

Peter
Peter on 11 Mar 2020
Like you thought, the cleanest process is to write a 5th script which will do as you've written above. If the code is easily converted into a function that sends back the data that gets plotted, you can save some time.
%This is pseudo code, but it should give you an idea for the flow
M = dir('mondayfiles/*xls');
T = dir('mondayfiles/*xls');
W = dir('mondayfiles/*xls');
R = dir('mondayfiles/*xls');
%Assuming you want to overlay the data:
Data = {M, T, W, R};
Lg = {'Monday','Tuesday','Wednesday','Thursday'};
fh = figure('Name','My Data');
ax = axes('Parent',fh);
hold(ax,'on');
for ix=1:numel(Data)
% Call your function "GetDataForPlot" to act on the files
x{ix}, y{ix} = GetDataForPlot(Data{ix});
plot(x{ix},y{ix},'DisplayName',Lg{ix});
end
hold off
xlabel('X Label Here');
ylabel('Y Label Here');
legend('show');
% If you need to do some processing with everyone together to make the 5th:
Data = {M, T, W, R};
for ix=1:numel(Data)
% Call your function "GetDataForPlot" to act on the files
x{ix}, y{ix} = GetDataForPlot(Data{ix});
end
% Call another function to add/multiply, or whatever needs to be done. You could
% also do it inline here. x and y are held in CELL arrays since I'm assuming they may not
% have the same size for each day of the week.
xFinal, yFinal = CombineData(x,y);
fh = figure('Name','My Data');
ax = axes('Parent',fh);
plot(xFinal, yFinal);
xlabel('X Label Here');
ylabel('Y Label Here');
legend('show');
Alternatively,
If you're pressed for time, there aren't a lot of data series, and you won't need to regenerate the data, another option is to open all four fig files, Pick one and Save-As your fifth file (important!), and then go to Tools-Edit Plot for each figure. You can literally select each data series and copy-paste it into the Fifth plot and then tune the colors/legend/axes by hand and save the fig-file at the end. If you need to manipulate the data from the data to get the 5th plot, then it's probably easier to do the first.
Unless you're just trying to concatenate everything? If so, append the data:
Data = {M, T, W, R};
x_all = [];
y_all = [];
for ix=1:numel(Data)
% Call your function "GetDataForPlot" to act on the files
x, y = GetDataForPlot(Data{ix});
x_all = [x_all, x]; % Assuming these structures are 1xN arrays
y_all = [y_all, y];
end
figure;
plot(x_all, y_all);

  1 Comment

Sun Kyoo Choi
Sun Kyoo Choi on 11 Mar 2020
It gives some sort of error. Can I show my code, and could you please tell me what I can improve?

Sign in to comment.


Translated by