How to plot (over one figure) one variable from each table located in 20 diffrent folders?

3 views (last 30 days)
Tomaszzz
Tomaszzz on 25 Feb 2022
Commented: Voss on 25 Feb 2022
Hi all,
I have 20 folders. In each folder there is a table. Each table contains variable 'x'. I would like to plot variable x from all folders over one figure. Some of my dirty logic below:
Step 1: I can so far list all folders:
% List all folders
topLevelFolder = pwd;
files = dir(topLevelFolder); % Get a list of folders in this folder.
dirFlags = [files.isdir];
subFolders = files(dirFlags); % Extract only those that are directories.
subFolders(1:2) = [];
Step 2: Next, I think I should list all tables from which variable x will be accessed
%%List all desired tables
for k = 1:length(subFolders)
all_tables = dir(fullfile(subFolders(1),'*_Cycles.mat')); % subfolder (1) beacuse the table I want to access in first on the list
end
Error using fullfile (line 67)
All inputs must be strings, character vectors, or cell arrays of character vectors.
Step 3: if step 2 works , I think I should load each table in a loop, load variable x, and plot in over one figure
%%Load each table, access variable x, and plot in over one figure
for k = 1:length(all_tables)
disp(all_tables(k).name)
A = fullfile(subFolders,all_tables(k).name);
variable_X = load(A);
end
Step 4: how to plot the x variable ?

Accepted Answer

Voss
Voss on 25 Feb 2022
% List all folders
topLevelFolder = pwd;
files = dir(topLevelFolder); % Get a list of folders in this folder.
dirFlags = [files.isdir];
subFolders = files(dirFlags); % Extract only those that are directories.
subFolders(1:2) = [];
% List all desired tables
all_tables = cell(1,numel(subFolders));
for k = 1:numel(subFolders)
temp = dir(fullfile(topLevelFolder,subFolders(k).name,'*_Cycles.mat'));
if isempty(temp)
continue
end
all_tables{k} = temp(1); % temp(1) beacuse the table I want to access in first on the list
end
% Load each table, access variable x, and plot in over one figure
figure();
for k = 1:numel(all_tables)
if isempty(all_tables{k})
continue
end
disp(all_tables{k}.name)
A = fullfile(topLevelFolder,subFolders(k).name,all_tables{k}.name);
% loads all variables from mat-file A, and stores them in struct
% variable_X:
variable_X = load(A);
% plots variable "X" (a field in the struct variable_X)
plot(variable_X.X);
hold on
end

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by