0
votes

I am trying to make a script in Matlab that pulls data from a file and generates an array of data. Since the data is a string I've tried to split it into columns, take the transpose, and split it into columns again to populate an array.

When I run the script I don't get any errors, but I also don't get any useful data. I tell it to display the final vector (Full_Array) and I get {1×4 cell} 8 times. When I try to use strsplit I get the error:

'Error using strsplit (line 80) First input must be either a character vector or a string scalar.'

I'm pretty new to Matlab and I honestly have no clue how to fix it after reading through similar threads and the documentation I'm out of ideas. I've attached the code and the data to read in below. Thank you.

clear
File_Name = uigetfile;      %Brings up windows file browser to locate .xyz file
Open_File = fopen(File_Name);          %Opens the file given by File_Name
File2Vector = fscanf(Open_File,'%s');  %Prints the contents of the file to a 1xN vector
Vector2ColumnArray = strsplit(File2Vector,';'); %Splits the string vector from 
                                                %File2Vector into columns, forming an array
Transpose = transpose(Vector2ColumnArray);  %Takes the transpose of Vector2ColumnArray 
                                            %making a column array into a row array
FullArray = regexp(Transpose, ',', 'split');

The data I am trying to read in comes from a .xyz file that I have titled methylformate.xyz, here is the data:

O2,-0.23799,0.65588,-0.69492;
O1,0.50665,0.83915,1.47685;
C2,-0.32101,2.08033,-0.75096;
C1,0.19676,0.17984,0.49796;
H4,0.66596,2.52843,-0.59862;
H3,-0.67826,2.36025,-1.74587;
H2,-1.03479,2.45249,-0.00927;
H1,0.23043,-0.91981,0.45346;
2

2 Answers

0
votes

When I started using Matlab I also had problems with the data structure. The last line

FullArray = regexp(Transpose, ',', 'split');

splits each line and stores it in a cell array. In order to access the individual strings you have to index with curly brackets into FullArray:

FullArray{1}{1} % -> 'O2'
FullArray{1}{2} % -> '-0.23799'
FullArray{2}{1} % -> 'O1'
FullArray{2}{2} % -> '0.50665'

Thereby the first number corresponds to the row and the second to the particular element in the row.

However, there are easier functions in Matlab which load text files based on regular expressions.

0
votes

Usually, the easiest function for reading mixed data is readtable.

data = readtable('methylformate.txt');    

However, in your case this is more complex because

  • readtable can't cope with .xyz files, so you'd have to copy to .txt
  • The semi-colons confuse the read and make the last column characters

You can loop through each row and use textscan like so:

fid = fopen('methylformate.xyz');
tline = fgetl(fid);
myoutput = cell(0,4);
while ischar(tline)
    myoutput(end+1,:) = textscan(tline, '%s %f %f %f %*[^\n]', 'delim', ',');
    tline = fgetl(fid);
end
fclose(fid);

Output is a cell array of strings or doubles (as appropriate).