Simple file I/O problem - help needed

1 回表示 (過去 30 日間)
Chris
Chris 2011 年 12 月 30 日
I have imported a file in this format:
"title1", "title2"
"somestring1", "5423423"
"somestring2", "091843"
(without the white spaces shown here)
I need to read this file and get the number. I can do this very simply in a simple programming language but I have trouble in Matlab.
All I can think of is make a function like this:
function [ ] = getN( file )
f = fscanf(file);
%Find and get the number between the 3rd and 4th " with a check like if fscanf(file, '%c') is " do that
end
Yes I am beginner, I don't even know how to print :P

回答 (1 件)

Friedrich
Friedrich 2011 年 12 月 30 日
Hi,
I created a test file (test.txt) with the following content:
"title1","title2"
"somestring1","5423423"
"somestring2","091843"
And in MATLAB I used:
fid = fopen('test.txt','r');
data = textscan(fid,'%*s %*c %f %*c','delimiter',',','headerlines',1);
fclose(fid);
numeric_val = data{1}
numeric_val =
5423423
91843

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by