从matlab中导入的文本文件在结构中创建结构
我不确定是否有人能帮助解决这个问题,但我们开始吧。我有4个文件夹,其中每个文件夹包含不同位置的数据,在文件夹中我有8个.txt文件,代表每个位置的测量变量(即每个位置测量的相同变量)。我正试图将这些数据导入matlab,并在astructure中列出测量的变量,以便以后可以相互比较和绘制(如果不这样做,它们将相互重写) 我已经编写了一个脚本,用于将这些内容导入matlab,该脚本工作正常,但并不完全符合我的要求,脚本如下所示:从matlab中导入的文本文件在结构中创建结构,matlab,structure,cell,Matlab,Structure,Cell,我不确定是否有人能帮助解决这个问题,但我们开始吧。我有4个文件夹,其中每个文件夹包含不同位置的数据,在文件夹中我有8个.txt文件,代表每个位置的测量变量(即每个位置测量的相同变量)。我正试图将这些数据导入matlab,并在astructure中列出测量的变量,以便以后可以相互比较和绘制(如果不这样做,它们将相互重写) 我已经编写了一个脚本,用于将这些内容导入matlab,该脚本工作正常,但并不完全符合我的要求,脚本如下所示: clear all pathName = 'E:\Universit
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,这似乎是不必要的。我意识到这是由于我写剧本最后一行的方式造成的,但我似乎无法改变它而不产生错误。如果您能就这个问题或脚本提供任何建议,我们将不胜感激。我认为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...
我不太清楚你喜欢哪种格式。您是否有
Location.Alaska.temp=1;Location.Alaska.lat=5;Location.Alaska.lon=3
而您更喜欢使用Location.Alaska=[1 3 5]
?目前,我说的是Location.Alaska.temp{1,1},我更喜欢temp不是1x1单元格,而是值。例如,当前如果我将Location.Alaska.temp写入命令窗口,matlab将返回[365x1 double],而我希望它返回向量。