textscan fopen

I have a file to open which looks as follows
row1: '#CIR,Date[L],Time[L],Type,Clr Dir.'
then a list of
rows 'AXY.P,20050101,09:35:01.202,quant,^'
I used fid=fopen('location of the file …) textscan(fid, ' …%s %f combinations …. yet couldn;t get out what I wanted in an output
I wish to have 5 columns as shown by the delimiter ',' and convert the last column sign^ to a number say 1. Ultimately it would look like
AXY.P 20050101 09:35:01.202, quant, 1
Any help is appreciated.

回答 (4 件)

Friedrich
Friedrich 2012 年 1 月 4 日

0 投票

Hi,
I created a file (data.txt) which contains the following lines
AXY.P,20050101,09:35:01.202,quant,^
AXY.P,20050101,09:35:01.202,quant,^
AXY.P,20050101,09:35:01.202,quant,^
You can get this data into MATLAB like this:
fid = fopen('data.txt','r');
data = textscan(fid,'%s%s%s%s%s','delimiter',',');
fclose(fid);
celldisp(data)
So convert the ^ to the number 4:
data{5} = num2cell(4*ones(size(data{5})));
celldisp(data)

2 件のコメント

Tiina
Tiina 2012 年 1 月 5 日
Hi,
with respect to the column containing character ^ (and some letters) num2cell may impose a value rather than convert or assign a number for characters and/or letters. if I use data.txt as created in the answer and I make an adjustment it may clarify the question more
AXY.P,20050101,09:35:01.202,quant,^
AXY.P,20050101,09:35:01.202,quant,w
AXY.P,20050101,09:35:01.202,quant,q
AXY.P,20050101,09:35:01.202,quant,^
then the char in the end would be in a col like
1
2
3
1
Tiina
Walter Roberson
Walter Roberson 2012 年 1 月 5 日
Use the three-output form of unique() and use the third output as the column value.

サインインしてコメントする。

TAB
TAB 2012 年 1 月 5 日

0 投票

Considering the content of file are as
AXY.P,20050101,09:35:01.202,quant,^
AXY.P,20050101,09:35:01.202,quant,w
AXY.P,20050101,09:35:01.202,quant,q
AXY.P,20050101,09:35:01.202,quant,^
-------------------------------------------------------
SymbolTable={'^','w','q'};
NumTable =[1 ,2 ,3];
fid = fopen('data.txt');
data = textscan(fid,'%s%s%s%s%s','delimiter',',');
for k=1:length(data{5})
data{5}(k)=num2cell(NumTable(strcmp(SymbolTable,data{5}(k))==1));
end
fclose(fid);
Tiina
Tiina 2012 年 1 月 5 日

0 投票

strfind() .... did the trick, thanks for your input as well
yair suari
yair suari 2012 年 1 月 5 日

0 投票

it looks like you want to use csvread

カテゴリ

ヘルプ センター および File ExchangeConvert Image Type についてさらに検索

タグ

質問済み:

2012 年 1 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by