Bingo Man
Bingo Man

Reputation: 65

Converting from Char to Struct : Error Matlab

When ever I am running this code; I am getting the error on matlab.

A = dir('D:\Folder_One\Folder_Two\'); 
    len = length(A);
    disp(len)
  P = [];
  Q = [];
  R = [];
    for n = 1: len
        filename = [' D:\Folder_One\Folder_Two\’,  A(n, :)];                   
        mydata = dlmread(filename);                                         
        Pfeatures = features(mydata(:5));                               
        Qfeatures = features(mydata(:7));                             
        P = [P;Pfeatures];
        Q = [Q;Qfeatures];
    end

??? Error using ==> horzcat The following error occurred converting from char to struct: Error using ==> struct Conversion to struct from char is not possible.

Upvotes: 0

Views: 3643

Answers (2)

Johnc
Johnc

Reputation: 26

Adiel's vote is right. A is a struct,and name is a part of A. you should fetch it from A. the first and second row of dir info,are '.' and '..'. you can also use

    if ~strcmpi( A(n).name,'.') and ~strcmpi( A(n).name,'..')

to escape first two struct

Upvotes: 1

Adiel
Adiel

Reputation: 3071

Variable A is a struct that contains the names, dates, and other parameters about files or folders in your directory, in different fields. If you want to loop over all files you should take only the field "name" from the struct, like this:

filename = [' D:\Folder_One\Folder_Two\’,  A(n).name];

You can't combine char with struct A(n), but A(n).name is char so you can combine it.

Another advice is to loop from 3 and not from 1, because if you look on the variable A, you will see that places 1 and 2 has "." and "..", for the current directory and its parent. I guess that you don't need it. If your files have names that start with some strange characters (!,@,#, etc.), so the dots can be in other places and you should skip it with if statement. Anyway, it a clean and neater way than just start from 3, but both should work.

Another advice from @DVarga is to skip all possible directories, if you have some, it can be easily determined by check A(n).isdir.

Upvotes: 1

Related Questions