MATLAB Answers

huda nawaf
0

problem in read txt file

Asked by huda nawaf
on 17 Apr 2012

hi, if I have txt file test.txt : 'poiu' 'grtf' 'yutr' 'trqw' 'tyui' 'poyt'

f=fopen(''test.txt'); d=fscanf(f,'%s'); I want d(1) be 'poiu' d(2) be 'grtf'.....etc

I do not want it d(1) be ' ,and d(2)be p

is there any solution for this problem?

thanks in advance, huda

  0 Comments

Tags

Products

No products are associated with this question.

1 Answer

Answer by Andrei Bobrov
on 17 Apr 2012
 Accepted answer

f = fopen('test.txt')
c = textscan(f,'%s');
fclose(f)
d = cellfun(@(x)x(2:end-1),c{1},'un',0)

EDIT

on comment

f = fopen('test.txt');
c = textscan(f,'%s','Delimiter',',');
fclose(f);
d = regexprep(c{1},'''','')

more variant

f = fopen('test.txt');
c = textscan(f,'%s','Delimiter','''');
fclose(f);
d = c{:}(2:2:end)

  4 Comments

I got

'poiugrtfyutr'
'trqwtyuipoyt'
this is not what I need
I need when write d(1) getn 'poiu' , d(2) get 'grtf' ...etc.

That contains your test.txt?

No this is txt file contains

'poiu' 'grtf' 'yutr'
'trqw' 'tyui' 'poyt'


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!