How to solve Reference to non-existent field 'folder'.

9 views (last 30 days)
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?

Accepted Answer

Stephen23
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
Skydriver
Skydriver on 3 Nov 2019
Thank you with your respond,
But in my coding I got the massage like this
Error using horzcat
The following error occurred converting from double to struct:
Conversion to struct from double is not possible.
Error in CalcAll (line 14)
X = [X,temp(:,2:end)];
Stephen23
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!