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

2 Ansichten (letzte 30 Tage)
Skydriver
Skydriver am 3 Nov. 2019
Bearbeitet: Stephen23 am 3 Nov. 2019
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?

Akzeptierte Antwort

Stephen23
Stephen23 am 3 Nov. 2019
Bearbeitet: Stephen23 am 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 Kommentare
Skydriver
Skydriver am 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 am 3 Nov. 2019
Bearbeitet: Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by