How to 'cat' my dataset?

16 ビュー (過去 30 日間)
Elias Unk
Elias Unk 2018 年 7 月 22 日
コメント済み: Walter Roberson 2018 年 7 月 22 日
I have .mat files in a folder, each of them is the same size 1x500, what i want to do is to create a table where the first element will be the file name(without the mat) and the second being the loaded data file so we'll end up having an nx2 matrix n being the number of files in the folder, what i tried:
full_val = 1;
path_directory='C:\Users\me\Desktop\test_data';
original_files=dir([path_directory '/*.mat']);
for k=1:length(original_files)
filename=[path_directory '/' original_files(k).name];
original_files(k).name = strtok(original_files(k).name, '.mat');
disp(original_files(k).name)
data_line = load(filename);
full_val = cat(1,full_val,filename);
end
but i get this error Error using cat Dimensions of matrices being concatenated are not consistent which makes sense but i have hundreds of file i can't write their names manually and load each individually how to adjust my code for the task?

採用された回答

Walter Roberson
Walter Roberson 2018 年 7 月 22 日
You load the data into the struct data_line, but yu do not save the loaded structure. Instead, you cat(1) something that starts out as the numeric value 1, together with something that is a character vector that is the file name.
Try
full_val = 1;
path_directory='C:\Users\me\Desktop\test_data';
original_files=dir([path_directory '/*.mat']);
nfiles = length(original_files);
full_val = cell(nfiles, 2);
for k=1:length(original_files)
filename=[path_directory '/' original_files(k).name];
original_files(k).name = strtok(original_files(k).name, '.mat');
disp(original_files(k).name)
data_line = load(filename);
full_val(k, :) = {filename, data_line};
end
Though what I suspect you would want would be
fn = fieldnames(data_line);
full_val(k, :) = {filename, data_line.(fn{1})};
This extracts the first variable's value out of the loaded file.
  3 件のコメント
Elias Unk
Elias Unk 2018 年 7 月 22 日
I figured it out somewhat like full_val(2,2) but when i do that it's giving me [1x1 struct] is that the right way or it should give the values in .mat for that file.
Walter Roberson
Walter Roberson 2018 年 7 月 22 日
full_val{2,2}
This will show a struct unless you use the variation I show with fieldnames.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by