在 matlab 中从导入的文本文件创建结构内的结构

发布于 2025-01-08 09:16:44 字数 1739 浏览 1 评论 0原文

我不确定是否有人可以帮助解决这个问题,但我们开始吧。我有 4 个文件夹,其中每个文件夹包含不同位置的数据,在文件夹中我有 8 个 .txt 文件,它们代表每个位置的测量变量(即在每个位置测量的相同变量)。我正在尝试将这些导入到 matlab 中,并在结构中列出测量的变量,以便之后可以将它们相互比较和绘制(如果不这样做,它们就会相互覆盖)。

我编写了一个脚本,用于将这些导入到 matlab 中,该脚本可以工作,但不完全按照我想要的方式,脚本如下:

clear all
pathName = 'E:\University\CEH Lancaster\Project\LA practice\final files';
FolderListing = dir(pathName);  
FolderListing = FolderListing(3:end);
    %lists the folder in the directory specified by pathName
for i = 1:length(FolderListing);
    LName{i} = (FolderListing(i).name);
        %obtains the name of each folder
end

for i = 1:length(LName)
    TopFolder{i} = fullfile(pathName,LName{i});
        %path for each individual folder 
    dirListing{i} = dir(fullfile(TopFolder{i},'*.txt'));  
        %list of the .txt files
    for ii = 1:length(dirListing{1,1});
        fileToRead1{1,i}{ii,1} = (dirListing{1,i}(ii,1).name);
        %name of the .txt files in the TopFolder
    end
end

for i = 1:length(fileToRead1);
    for ii = 1:length(fileToRead1{1});
    fid{1,i}{ii,1} = fopen((fullfile(TopFolder{1,i},fileToRead1{1,i}{ii,1})));
        %open the files specified by fileToRead prior to importing the data
        %into matlab
    data{1,i}{ii,1} = textscan(fid{1,i}{ii,1},'%f');
        %import the data into matlab
    [~,name{1,i}{ii,1}] = fileparts(fileToRead1{1,i}{ii,1});
        %obtain the name of each of the variables
    Location.(LName{i}).(genvarname(name{1,i}{ii,1})) = data{1,i}{ii,1};
        %create a strucutre for the individual locations and the
        %variables.
    end
end

问题在于最终结果,而不是有 Location.Name 然后是列表变量,我有 Location.Name.variables,这似乎没有必要。我意识到这是由于我编写脚本最后一行的方式造成的,但我似乎无法在不产生错误的情况下更改它。如果您能就该问题或一般脚本提供任何建议,我们将不胜感激。

I'm not sure if anyone can help with this question but here we go. I have 4 folders where each folder contains data for different locations, within the folders I have 8 .txt files which represent the measured variables at each location (i.e. same variables measured in each location). I'm trying to import these into matlab and list the measured variables in astructure so they can be compared and plotted against one another afterwards (without doing this they will over write one another).

I've written a script for importing these into matlab, the script works but not exactly in the way I want it to, the script is as follows:

clear all
pathName = 'E:\University\CEH Lancaster\Project\LA practice\final files';
FolderListing = dir(pathName);  
FolderListing = FolderListing(3:end);
    %lists the folder in the directory specified by pathName
for i = 1:length(FolderListing);
    LName{i} = (FolderListing(i).name);
        %obtains the name of each folder
end

for i = 1:length(LName)
    TopFolder{i} = fullfile(pathName,LName{i});
        %path for each individual folder 
    dirListing{i} = dir(fullfile(TopFolder{i},'*.txt'));  
        %list of the .txt files
    for ii = 1:length(dirListing{1,1});
        fileToRead1{1,i}{ii,1} = (dirListing{1,i}(ii,1).name);
        %name of the .txt files in the TopFolder
    end
end

for i = 1:length(fileToRead1);
    for ii = 1:length(fileToRead1{1});
    fid{1,i}{ii,1} = fopen((fullfile(TopFolder{1,i},fileToRead1{1,i}{ii,1})));
        %open the files specified by fileToRead prior to importing the data
        %into matlab
    data{1,i}{ii,1} = textscan(fid{1,i}{ii,1},'%f');
        %import the data into matlab
    [~,name{1,i}{ii,1}] = fileparts(fileToRead1{1,i}{ii,1});
        %obtain the name of each of the variables
    Location.(LName{i}).(genvarname(name{1,i}{ii,1})) = data{1,i}{ii,1};
        %create a strucutre for the individual locations and the
        %variables.
    end
end

The problem lies in the final outcome where instead of having Location.Name and then the list of variables, I have Location.Name.variables, which doesn't seem necessary. I realise that its due to the way I've written the last line of the script but I can't seem to change it without it producing an error. Any advice you could give on the problem or on the script in general would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2025-01-15 09:16:44

我认为 cell2mat 是您为此目的所需的功能。这是我的用法,看看是否符合你的需求:

tt = {ones(1,100)};
tt
tt = 

    [1x100 double]
cell2mat(tt)
    ans =

      Columns 1 through 15

         1     1     1     1     1     1     1     1     1     1     1     1...

I think cell2mat is the function you want for this purpose. Here's my usage, see if it fits your needs:

tt = {ones(1,100)};
tt
tt = 

    [1x100 double]
cell2mat(tt)
    ans =

      Columns 1 through 15

         1     1     1     1     1     1     1     1     1     1     1     1...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文