MATLAB: Delimiting a text file like “Text to columns” and viewing the output

delimitMATLABtext file

Hello, I am trying to import data from a Text file (.txt). This data has a single column of mixed (numeric and string) data that is | (bar) delimited. My goal is to import this data and seperate each delimited portion into a seperate columns while maintaining the rows. Essentially, I am trying to do the Excel "Text to columns" with MATLAB. Thank you
Below is a step-by-step output:
%open file
fid = fopen(YourFile,'rt')
fid =
5
%figure out how many columns are there
firstline = fgetl(fid)
firstline =
RMS Functional Block|Reference Designator|Part Number|Sheet Number|Description|Invisible
ncol = 1 + sum(firstline == '|')
ncol =
6
%reset to beginning of file
fseek(fid,0,0)
ans =
0
%read data
indata = textscan(fid,repmat('%s',1,ncol),'Delimiter','|','CollectOutput',1)
indata = {646×6 cell}
%close file
fclose(fid)
ans =
0
How do I look at indata's data?

Best Answer

The data is a cell array. You can:
1. type indata{1,1} to see the first element
2. use celldisp(indata) to see all
3. use open('indata') to see all
Related Question