problems substituting dataread with textscan

3 views (last 30 days)
Hello everyone
i was given a script which enables me to extract a normalization value from a data file. the data file consist of a header with two columns and a body with three columns of data. the code is shown below
C = dataread('file', 'test.dat', '%s', 'delimiter', '\n') % data into array
string=validatestring('GlobalNormalization=',C) % line with GN into new string
v = sscanf(string,'%*s%f') % puts floating point from the new string into v
It works beautifully but matlab underlines "dataread" and tells me that it will be removed in future releases. Matlab suggests that i instead use tesxtscan. i threfore tried this - the code can be seen below
fid = fopen('test.dat','r');
C = textscan(fid, '%s','delimiter', '\n');
string=validatestring('GlobalNormalisation=',C);
v = sscanf(string,'%*s%f');
fclose(fid);
this gives me the error message:
"Error using validatestring (line 61) Second argument must be a cell array of allowed strings.
Error in Import_Int (line 9) string=validatestring('GlobalNormalisation=',C);"
I think the reason for this is that dataread simply creates a variable called C wheras textscan creates a 1x1 cell called C. Can anyone tell me how to simply create a variable called C using textscan or suggest some other way of working around the problem?
cheers

Accepted Answer

Martin Nors Pedersen
Martin Nors Pedersen on 18 Jan 2013
using
string=validatestring('GlobalNormalisation=',C{1,1});
instead of
string=validatestring('GlobalNormalisation=',C);
solved the problem. i should probably have realized this immediatly. but it got abit confused about the array inside a specific cell position
i am still interested in learning if you can avoid the cell structure and instead just create a variable when you use textscan

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!