1
votes

I have a program which was designed to be used through the MatLab GUI that I need to use via command line code. I am having trouble getting it to read in the file properly. The program requires the file to be a cell array of strings in a two-dimensional format (i.e. 40x10 array of strings). When using the MatLab GUI, I simply use "Import Data" to highlight the 40x10 area and upload as a Cell Array (Text option: String Array; appears as a 40x10 cell array in the Workspace).

Via Code: When using the following 'textscan' MatLab code: CellArray = textscan(FileName,'%s%s%s%s%s%s%s%s%s%s','Delimiter',',');

And using the "size" command to determine its dimensions ([m, n] = size(Features)), it seems as though it is being imported as a 1x10 array rather than a 40x10 array.

How do I upload the file as a 40x10 cell array via code (not via the GUI) in the manner I described? Thank you.

2

2 Answers

1
votes

If the file is a text file in which each line has 10 strings separated by commas, you could try reading each line with fgetl, splitting it at the commas with split(line, ','), converting to a cell cellstr and finally placing the results in the corresponding row. Something like:

features = cell(40,10);
fid=fopen('filename');
line="just a place keeper";
k = 1;
while ischar(line)
    line=fgetl(fid);
    features(k,:) = cellstr(split(line, ','))'; % Note the ' = transpose into a row
    k = k+1;
end
close(fid);

Hope this helps

JAC

0
votes

Using the "CollectOutput" option for MatLab's "textscan" option corrected the is

opt = {'CollectOutput',true};
fmt = '%s%s%s%s%s%s%s%s%s%s';
C = textscan(fid,fmt,opt{:});
C = C{1};