将多个文件中的数据附加到 matlab 数组

发布于 2024-12-05 12:56:58 字数 842 浏览 1 评论 0原文

您好,我需要您帮助将从许多文件中读取的所有数据附加到矩阵中。我已经制作了以下脚本,

path='C:\Users\Kostas\Documents\MATLAB\';
filefolder=strcat(path,'MSL*.txt');
files=dir(filefolder);
k=0;
for i=1:length(files)
    filename=strcat(path,files(i).name);    
    %load the filename and create vectors of height (Z),
    %lat and lon
    newData=importdata(filename,'\t', 1);
    vars = fieldnames(newData);
for j = 1:length(vars)
    assignin('base', vars{j}, newData.(vars{j}));
end 
    timeas=data(:,1);
    lat=data(:,2);
    lon=data(:,3);
    Z=data(:,4);
  %  daten=(timeas/24)+doy;
   k=k+1; 
%append data to matrix Teff_series
    Teff_series(k,:)= [timeas lat lon Z];
end

当我运行此脚本时收到的错误消息是

??? Subscripted assignment dimension mismatch.

Error in ==> te at 31
    Teff_series(k,:)= [lat lon Z];

提前感谢

Hello i need you help to append all the data i read from many files into a matrix. I have made the following script

path='C:\Users\Kostas\Documents\MATLAB\';
filefolder=strcat(path,'MSL*.txt');
files=dir(filefolder);
k=0;
for i=1:length(files)
    filename=strcat(path,files(i).name);    
    %load the filename and create vectors of height (Z),
    %lat and lon
    newData=importdata(filename,'\t', 1);
    vars = fieldnames(newData);
for j = 1:length(vars)
    assignin('base', vars{j}, newData.(vars{j}));
end 
    timeas=data(:,1);
    lat=data(:,2);
    lon=data(:,3);
    Z=data(:,4);
  %  daten=(timeas/24)+doy;
   k=k+1; 
%append data to matrix Teff_series
    Teff_series(k,:)= [timeas lat lon Z];
end

the error message i get when i run this script is

??? Subscripted assignment dimension mismatch.

Error in ==> te at 31
    Teff_series(k,:)= [lat lon Z];

Thanks in advance

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

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

发布评论

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

评论(2

丶视觉 2024-12-12 12:56:59

让我举个例子:

%# get the list of files
fpath = 'C:\Users\Amro\Desktop\';
files = dir( fullfile(fpath,'file*.dat') );
files = strcat(fpath,{files.name}');

%# read data from all files and store in cell array
Teff_series = cell(numel(files),1);
for i=1:numel(files)
    newData = importdata(files{i}, '\t', 1);
    Teff_series{i} = newData.data;
end

%# combine all into a matrix
data = vertcat(Teff_series{:});
colNames = newData.colheaders;

%# extract columns as vectors
t = data(:,1);
lat = data(:,2);
lon = data(:,3);
Z = data(:,4);

如果我使用这些示例数据文件:

file1.dat

t   lat lon Z
1   2   3   4
2   3   4   5
4   5   6   6

file2.dat

t   lat lon Z
4   5   6   6
2   3   4   5
1   2   3   4

我得到以下结果:

>> colNames
colNames = 
    't'    'lat'    'lon'    'Z'

>> data
data =
     1     2     3     4
     2     3     4     5
     4     5     6     6
    40    50    60    60
    20    30    40    50
    10    20    30    40

Let me give an example:

%# get the list of files
fpath = 'C:\Users\Amro\Desktop\';
files = dir( fullfile(fpath,'file*.dat') );
files = strcat(fpath,{files.name}');

%# read data from all files and store in cell array
Teff_series = cell(numel(files),1);
for i=1:numel(files)
    newData = importdata(files{i}, '\t', 1);
    Teff_series{i} = newData.data;
end

%# combine all into a matrix
data = vertcat(Teff_series{:});
colNames = newData.colheaders;

%# extract columns as vectors
t = data(:,1);
lat = data(:,2);
lon = data(:,3);
Z = data(:,4);

If I use these sample data files:

file1.dat

t   lat lon Z
1   2   3   4
2   3   4   5
4   5   6   6

file2.dat

t   lat lon Z
4   5   6   6
2   3   4   5
1   2   3   4

I get the following results:

>> colNames
colNames = 
    't'    'lat'    'lon'    'Z'

>> data
data =
     1     2     3     4
     2     3     4     5
     4     5     6     6
    40    50    60    60
    20    30    40    50
    10    20    30    40
摘星┃星的人 2024-12-12 12:56:59

该错误表明等号表达式的左侧 - 在本例中:
Teff_series(k, :) 的

大小与右侧的大小不同:

[lat lon Z]

调试此问题的一种方法是执行命令:

dbstop if all error

然后重新运行脚本。它将在引发错误的位置停止调试器,然后您可以找出大小差异。

希望这有帮助。

The error indicates that the left hand side of the equal expression - in this case:
Teff_series(k, :)

is of a different size than the right hand side:

[lat lon Z]

One way to debug this issue is execute the command:

dbstop if all error

and then re-run your script. It will stop the debugger at the point where the error is thrown and then you can figure out the difference in sizes.

Hope this helps.

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