convert a struct to a mxn cell array

hi,
Let say i have 6 files: 101 ... 103 and 201 ... 203 which represent 2 people (p) with 3 different expressions(e). After using 'dir' to gel all the files, i try to create a cell (2x3). However i got an error ' Improper index matrix reference '. Below is the code:
FolderTrain = 'myfolderpath'; %specify the folder
AllTrainFiles = fullfile(FolderTrain, '*.txt'); % get the directory path for all files
myData = dir(AllTrainFiles);
for i=1:length(myData)
j=2;
k=3;
TrainData = cell(j,k);
end
cell
for p = 1:j
for e = 1:k
baseTrainFilename = {TrainData(p,e).name};
TrainFilename = fullfile(FolderTrain, baseTrainFilename);
fid = fopen(TrainFilename);
A = fscanf(fid, '%d %d %d');
TrainData{p,e} = A;
fclose(fid);
end
end
How can i create a correct 2x3 cell? Thank you.

1 件のコメント

Walter Roberson
Walter Roberson 2017 年 11 月 23 日
Why are you running the body of
for i=1:length(myData)
j=2;
k=3;
TrainData = cell(j,k);
end
more than once? It always assigns the same content to the variables.

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

 採用された回答

dpb
dpb 2017 年 11 月 23 日
編集済み: dpb 2017 年 11 月 24 日

0 投票

FolderTrain = 'myfolderpath'; % specify the folder
AllTrainFiles=fullfile(FolderTrain,'*.txt'); % full path wild card pattern for all files
d=dir(AllTrainFiles); % directory list for all files
for i=1:length(d) % iterate over files; Matlab does return sorted order
data{i}=importdata(fullfile(FolderTrain,d(i).name)); % read each file as array into cell
end
data=reshape(data,3,2); % rearrange as 2 columns, 3 rows by sequential order
The above presumes the data files are numeric arrays as would be implied by the format string used in the fscanf call.

5 件のコメント

yan abr
yan abr 2017 年 11 月 23 日
i'm sorry sir, how can i get an error 'Undefined variable "fullfile" or class "fullfile" using above code. I fix
data{i}=importdata(fullfile{FolderTrain,d(i).name});
already.
Walter Roberson
Walter Roberson 2017 年 11 月 24 日
data{i} = importdata(fullfile(FolderTrain,d(i).name)); % read each file as array into cell
yan abr
yan abr 2017 年 11 月 24 日
thank you very much sir :)
dpb
dpb 2017 年 11 月 24 日
編集済み: dpb 2017 年 11 月 24 日
From the original lines
baseTrainFilename = {TrainData(p,e).name};
TrainFilename = fullfile(FolderTrain, baseTrainFilename);
I'd pasted the fullfile() argument text into the place where the .name was used to pick it up to eliminate the unneeded temporary; unfortunately it was inside the existing curlies...old eyes and all that make them hard to tell apart... :(
yan abr
yan abr 2017 年 11 月 25 日
its ok sir..really thankful for your help :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

タグ

質問済み:

2017 年 11 月 23 日

コメント済み:

2017 年 11 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by