Clear Filters
Clear Filters

Problem while using textscan

1 view (last 30 days)
Shelender Kumar
Shelender Kumar on 26 Oct 2018
Commented: Star Strider on 31 Oct 2018
My file has a string as the first row and numeric values as other rows, I can get rid of the header by using formatspec and N as given textscan help. I do not want to write %f four times while using the textscan again to read the numeric data. I have given the test code and please find the sample file. Thanks for helping
filename = 'S.txt'; fileID = fopen(filename); formatSpec = '%s'; N = 4; C_text = textscan(fileID,formatSpec,N); %% Read the numeric data in the file. C_data0 = textscan(fileID,'%f %f %f %f','CollectOutput',1); %% Here I do not want to write %f 4 times

Accepted Answer

Star Strider
Star Strider on 26 Oct 2018
Edited: Star Strider on 26 Oct 2018
If you do not want to type the format string, just use the dlmread funciton:
S = dlmread('S.txt', '\t', 1, 0)
S =
1 1 1 1
2 2 2 2
3 2 3 3
There are many ways to read files in MATLAB.
  18 Comments
Shelender Kumar
Shelender Kumar on 31 Oct 2018
Thanks a lot it helps
Star Strider
Star Strider on 31 Oct 2018
As always, my pleasure.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!