How to solve Reference to non-existent field 'folder'.
2 views (last 30 days)
Show older comments
I have a coding like this
folder='D:\\MultiLinierRegression\OutputRd'
fils=dir([folder '\*.txt']); % lists all the files with a .txt extension in myfolder
[~,idx]=sort({fils.name}); % index to load .txt files alphabetically
temp=importdata([fils(idx(1)).folder '\' fils(idx(1)).name]); % read A.txt
Y=temp(:,1); X=temp(:,2:end);
for ii=idx(2:end)
temp=importdata([fils(ii).folder '\' fils(ii).name]);
X =[X temp(:,2:end)];
end
But I got a message like this
Reference to non-existent field 'folder'.
Error in CalcRdAll (line 8)
temp=importdata([fils(idx(1)).folder '\' fils(idx(1)).name]); % read A.txt
Is there any one can help?
0 Comments
Accepted Answer
Stephen23
on 3 Nov 2019
Edited: Stephen23
on 3 Nov 2019
The folder field (and recursive folder searching) was added in R2016b.
As you are not using the recursive folder search you can simply do this:
D = 'D:\\MultiLinierRegression\OutputRd'
S = dir(fullfile(D,'*.txt'));
[~,idx] = sort({S.name}); % index to load .txt files alphabetically
temp = importdata(fullfile(D,S(1).name));
Y = temp(:,1);
X = temp(:,2:end);
for ii = idx(2:end)
temp = importdata(fullfile(D,S(ii).name));
X = [X,temp(:,2:end)];
end
2 Comments
Stephen23
on 3 Nov 2019
Edited: Stephen23
on 3 Nov 2019
Apparently your file data do not all have the same sizes. You can avoid this error by importing into a cell array or a structure, e.g.:
D = 'D:\\MultiLinierRegression\OutputRd'
S = dir(fullfile(D,'*.txt'));
for ii = 1:numel(S)
S(ii).data = importdata(fullfile(D,S(ii).name));
end
After the loop you can investigate the sizes, adjust the sizes, and concatenate if possible.
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!