如何在Matlab中从两个不同的子文件夹绘制到同一图中?

huangapple go评论57阅读模式
英文:

How do I plot from two separate subfolders in Matlab onto the same plot?

问题

C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase1\out\Stats

C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase2\out\Stats

stats 文件夹中有 10 个以 1 到 10 编号的 txt 文件。我如何在同一图中绘制两个子文件夹中的 '1.txt' 文件,但在图例中将图命名为 Phase1 和 Phase2?相同编号的所有文件具有相同的维度。因此,Phase1\out\Stats\8.txt 和 Phase2\out\Stats\8.txt 具有相同的维度。目前数据存储在一个 fds 中,因此我正在使用 'cellfun' 逐步绘制所有 20 张表格的单元数组。

由于在 Phase 和 Stats 之间还有一个子文件夹,我是否应该将 Stats 保存到其相应的 Phase 子文件夹中,并从那里开始工作,因为 'out' 中还有其他文件?预先考虑,最简单的方法是使用下拉菜单,因为以后我想要绘制所有 Phase,但用户可以选择例如 '3.txt',这是所有 '3.txt' 文件都具有的共同变量,并绘制所有 3.txt 文件的该变量。

我看到的主要问题是,我的 fds 中的数据存储在一个 20x1 表格单元数组中,但我不知道如何按文件夹工作,例如,一个 2x1 单元数组,每行包含一个 10x1 表格数组。我尝试更改图例,但这会将完整路径名加载到文本框中,覆盖了大部分绘图。

老实说,我不太擅长 Matlab,不知道接下来该怎么做。

Folders = dir(fullfile("C:\Users\NamE\OneDrive\Desktop\Files\Data\*\*\**\Stats\","*.txt"));

Folders = Folders(~ismember({Folders.name},{'.','..'}));

Folders_Data = struct2cell(Folders);
Folders_Data = transpose(Folders_Data);

Files_Data = struct2cell(Folders);
Files_Data = transpose(Files_Data);
Files_Data = Files_Data(:,2);
Files_Data = unique(Files_Data);

fds = fileDatastore(Files_Data, ReadFcn=@(x) readtable(x,VariableNamingRule="preserve"), FileExtensions=".txt", IncludeSubfolders=true, ReadMode="file"); 
data = readall(fds)
英文:

I have 2 subfolders that I would like to plot in Matlab. They are stored as such:

C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase1\out\Stats

C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase2\out\Stats

The stats folders have 10 txt files in them numbered 1 to 10. How can I plot file '1.txt' from both subfolders on the same figure, but in the legend have the plots named Phase1 and Phase2? All files of the same number have the same dimensions. So Phase1\out\Stats\8.txt and Phase2\out\Stats\8.txt have the same dimensions. Currently the data is in a fds and hence cell array of all 20 tables that I am plotting as I go using 'cellfun'

Because there is another subfolder between Phase and Stats, should I save Stats into its respective Phase subfolder and work from there as there is other files in 'out'? Thinking ahead, what would be the easiest way to work a dropdown as later on I want to have it so all Phases will be plotted but the user can select, e.g '3.txt' a variable common to all '3.txt' files as they are all the same, and will plot that variable of all the 3.txt files.

The main problem I see is that the data from my fds is stored in a 20x1 cell array of tables but I don't know how to work it be folder, e.g. a 2x1 cell array each row containing a 10x1 array of tables. I tried changing up the legend but that loaded the full path name into the text box which covered a large percentage of the plot.

Honestly, not skilled enough at Matlab to know where to go from here.

Folders = dir(fullfile("C:\Users\NamE\OneDrive\Desktop\Files\Data\*\*\**\Stats\","*.txt"));

Folders = Folders(~ismember({Folders.name},{'.','..'}));

Folders_Data = struct2cell(Folders);
Folders_Data = transpose(Folders_Data);


Files_Data = struct2cell(Folders);
Files_Data = transpose(Files_Data);
Files_Data = Files_Data(:,2);
Files_Data = unique(Files_Data);

fds = fileDatastore(Files_Data, ReadFcn=@(x) readtable(x,VariableNamingRule="preserve"), FileExtensions=".txt", IncludeSubfolders=true, ReadMode="file"); 
data = readall(fds)

;

答案1

得分: 2

以下是翻译好的代码部分:

root1 = 'C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase1\out\Stats';
root2 = 'C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase2\out\Stats';

for i = 1:10
   file_name = sprintf('%d.txt',i);
   f1 = fullfile(root1,file_name);
   f2 = fullfile(root2,file_name);

   %Load both files
   data1 = ...
   data2 = ...

   figure(i)
   plot(data1)
   hold on
   plot(data2)
   hold off
   legend({'Phase1','Phase2'})
   title(sprintf('Results from file %d',i))
end
d = dir(fullfile("C:\Users\NamE\OneDrive\Desktop\Files\Data\*\*\**\Stats\","*.txt"));

%This really is a folder name
folders = {d.folder};

%Find all Phase### folders
temp = regexp(folders,'Phase(\d+)','once','tokens');

n_files = length(temp);
folder_id = NaN(n_files,1);
for i = 1:length(temp)
    cur_match = temp{i};
    if ~isempty(cur_match)
       folder_id(i) = str2double(cur_match{1});
    end
end

%folder_id : holds ## for each phase
max_phase = max(folder_id);

%Shuffling data as requested
data2 = cell(1,max_phase);
for i = 1:max_phase
    data2{i} = data(folder_id == i);
end
英文:

Here's how I would do it ...

root1 = 'C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase1\out\Stats';
root2 = 'C:\Users\Name\OneDrive\Desktop\Outputs\Data\Phase2\out\Stats';

for i = 1:10
   file_name = sprintf('%d.txt',i);
   f1 = fullfile(root1,file_name);
   f2 = fullfile(root2,file_name);

   %Load both files
   data1 = ...
   data2 = ...

   figure(i)
   plot(data1)
   hold on
   plot(data2)
   hold off
   legend({'Phase1','Phase2'})
   title(sprintf('Results from file %d',i))
end 

Edit: Here's something that a bit more directly answers the question. Note that the result of dir is a structure whereas the name Folders in the question implies it is a list of folders. I tend to use the variable d as the output from dir

d = dir(fullfile("C:\Users\NamE\OneDrive\Desktop\Files\Data\*\*\**\Stats\","*.txt"));

%This really is a folder name
folders = {d.folder};

%Find all Phase### folders
temp = regexp(folders,'Phase(\d+)','once','tokens');

n_files = length(temp);
folder_id = NaN(n_files,1);
for i = 1:length(temp)
    cur_match = temp{i};
    if ~isempty(cur_match)
       folder_id(i) = str2double(cur_match{1});
    end
end

%folder_id : holds ## for each phase
max_phase = max(folder_id);

%Shuffling data as requested
data2 = cell(1,max_phase);
for i = 1:max_phase
    data2{i} = data(folder_id == i);
end

huangapple
  • 本文由 发表于 2023年7月27日 21:58:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定