Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

importing a text file into a vector

Status
Not open for further replies.

ill2000m

Electrical
Oct 7, 2003
7
I have a text file and i would like to read its content into a vector x in my m file, i looked at help section in Matlab and tried many function but it still doesnt work, does anyone know how to do it .. e.g

content of my text file is and its in the directory C:\work\work.txt

A former senior Partner whose association with the firm dates back to 1958, John "retired" in 1995 only to come back as the firm's Specialist Consultant in Clinical Negligence. A member of the Law Society Clinical Negligence panel, John has a formidable reputation both locally and regionally.

Educated at Gordonstoun and Liverpool University, John qualified as a solicitor in 1958 after "articles" in a practice on Merseyside.



i tried to command in my m file as

[x] = textread('C:\work\work.txt ','%c')

i am not quite sure which format i should use, but all the content in my text file are ASCII character ...

 
Replies continue below

Recommended for you

Try using importdata, such as:

x=importdata('hey.txt');

If the file hey.txt contains the text: "Hey, how are u",

Matlab will display in the command window (upon entering 'x'):

'Hey, how are u'
 
Thanks very much to P4t for the replied ... i know the command

x=importdata('hey.txt');

can display the content of hey.txt on the command window.. but i want the number of characters inside the file hey.txt store into the variable x so that i can use the variable x to do some other task, do u know how to do it ??
 
I would start by opening the text file, then reading in the text as a large text string. You can then manipulate the string to find what you want.

fid = fopen('C:\work\work.txt ');
b=fscanf(fid,'%c')
c = length(b)
fclose(fid)

This one just finds the length of the string (including whitespace characters).

The trouble with textread is that it automatically assumes you want to ignore whitespace, or use whitespace as delimiters.

Don't know what you want to do with the string, but this should at least get it into matlab to manipulate it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor