필터 지우기
필터 지우기

need function to save my data

조회 수: 1 (최근 30일)
huda nawaf
huda nawaf 2012년 9월 14일
hi,
I have data is formatted as
*cluster1 2 3
cluster2 1 4 5
cluster3 6 7 10 8*
I would like to find way to save it in file, I know textscan work with such format but this function need equal number of columns in each row.
is there any other way to do that?
thanks
  댓글 수: 4
Walter Roberson
Walter Roberson 2012년 9월 14일
Try str2num(c2{1})
huda nawaf
huda nawaf 2012년 9월 14일
thanks when I do that , it still deal with 23 as string where when need x1{1}(1) will give me 23 not 2

댓글을 달려면 로그인하십시오.

답변 (4개)

per isakson
per isakson 2012년 9월 14일
편집: per isakson 2012년 9월 15일
I assume that
  1. you want to import data from a text file, which is defined by the three rows embraced by "*"
  2. the "*" are traces of your effort to format the text
Since the rows have different formats textscan is not appropriate.
One way to read the file is to loop over the all rows and read one row at a time with the function
fgetl, Read line from file, removing newline characters
There is an example in the documentation
.
-- Working code [ R2012a, 2012-09-15 ] ---
>> S = cssm()
S =
1x3 struct array with fields:
Cluster
Items
>> [S.Cluster]
ans =
1 2 3
>> S(3).Items
ans =
6 7 10 8
where cssm.m is
function S = cssm()
fid = fopen('cssm.txt');
cup = onCleanup( @() fclose( fid ) );
S = struct([]);
str = fgetl( fid );
while ischar( str )
str = strtrim( str );
str = strrep( str, 'cluster', '' );
num = str2num( str );
S(end+1).Cluster = num(1); %#ok<AGROW>
S(end ).Items = num(2:end);
str = fgetl( fid );
end
end
and where cssm.txt contains the tree rows
cluster1 2 3
cluster2 1 4 5
cluster3 6 7 10 8
.
--- Alternate code ---
>> S = cssm()
S =
1x3 struct array with fields:
RowHeader
Items
>> {S.RowHeader}
ans =
'cluster1' 'cluster2' 'cluster3'
>> S(3).Items
ans =
6 7 10 8
where cssm.m is
function S = cssm()
fid = fopen('cssm.txt');
cup = onCleanup( @() fclose( fid ) );
S = struct([]);
str = fgetl( fid );
while ischar( str )
[ tok, str ] = strtok( strtrim( str ) );
S(end+1).RowHeader = tok; %#ok<AGROW>
S(end ).Items = str2num( str );
str = fgetl( fid );
end
end
  댓글 수: 1
per isakson
per isakson 2012년 9월 15일
Working code added

댓글을 달려면 로그인하십시오.


Pritesh Shah
Pritesh Shah 2012년 9월 14일
Just find help save and load function.
If you want to save data....
Assuming that it is already available in work space.

Image Analyst
Image Analyst 2012년 9월 15일
And the problem with using save() is......??????? What????

Pritesh Shah
Pritesh Shah 2012년 12월 7일
Use save function to store variables.
Help save
Enjoy !!

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by