0

I have several folders. In every folder is 1 file called Data. Every file has structures, one of those is Data.ensemble. From this structure I extract the temperature (column 9, see question Hierarchy between and / or in an if statement) . My problem now is within my loop, to extract and save this temperature in a structure every iteration. For example, if I have 10 folders and 10 files. I want

Temperature.1=[12 15 10...]
Temperature.2=[20 30 11...]
...
Temperature.10=[15 26 27...]

and these will all be different sizes. The extraction works fine, it is saving it that is a problem. I tried the following code with help from https://nl.mathworks.com/matlabcentral/answers/304405-create-a-struct-iteratively

folders =struct2cell(dir(Pathfolders)); %Pathfolders is the path to the folders
NaamFolders=string(char(folders(1,:))); %make a list of all the foldernames
lenNF=length(NaamFolders);

for i = 1:lenNF
    Map=char(NaamFolders(i,1)); % Give me the name of the first , second, ... folder
    Pathfiles=[Path,'\',Map,'\','*_new.mat']; %Make the path to the files that end with _new.mat (in my case only 1)
    files =struct2cell(dir(Pathfiles)); %Select all files that end with _new.mat (in my case only 1)
    NaamFiles=char(files(1,:)); % Make a list of the names of all those files (in my case only 1)
    FilePath=[Path,'\',Map,'\',NaamFiles]; %create path to that 1 file
    Data=load(FilePath); %load that file
    [lenData,lenvariables]=size(Data.ensemble) % determine the size of the file structure called ensemble
    idx=NaamFolders{i} %Select the name you want to call your structure in the variable you want to create
    index = ismember(Data.ensemble(:,8),[10,20,30]) & Data.ensemble(:,5) == 12; %determine the index where  column 5 == 12 and column 8 == 10 or 20 or 30
    RightData   = Data.ensemble(index,:) % this the data where the previous comment is true
    Temperature.idx=RightData(:,9) % this line does not work. For every iteration, create a new structure within temperature that extracts column 9 from RightData.

end

It is the last line that does not work and gives the error

Unable to perform assignment because dot indexing is not supported for variables of this type.
Error in ExtractTrajectory (line 36)
    Temperature.idx=RightData(:,9)
Jellyse
  • 839
  • 7
  • 22
  • I believe you need some parentheses, as in https://uk.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html - so your line would have `Temperature.(idx) = ` – etmuse Jun 12 '19 at 14:32
  • @etmuse still the same error. – Jellyse Jun 12 '19 at 14:38

1 Answers1

0
  • Variable name should start always with a letter.
  • space, number , special caracters such as ~, #, % are not allowed.

Check NaamFolders format or print its format for us.

what you can do if it starts with number is just add a given letter to all NaamFolders list elements as follows

NaamFolders = strcat('z',NaamFolders)

Struct field format requirement


case 1: starting with letter

>> isvarname("folder1")

ans =

  logical

   1

can be used with dot assignment

>> check.("folder1") = 5

check = 

  struct with fields:

    folder1: 5


case 2: starting with number


>> isvarname("1folder")

ans =

  logical

   0

can't be used with dot assignment

>> check.("1folder") = 5
Invalid field name: '1folder'.

case 3: starting with space

>> isvarname("    folder")

ans =

  logical

   0

can't be used with dot assignment

>> check.("    folder") = 5
Invalid field name: '    folder'.

case 4: starting with special character

>> isvarname("#folder")

ans =

  logical

   0

can't be used with dot assignment

>> check.("#folder") = 5
Invalid field name: '#folder'.
Community
  • 1
  • 1
Adam
  • 2,726
  • 1
  • 9
  • 22